python搬砖狗

导航

 

 

 

 

在app中注册表的时候使用到的方法为:

admin.site.register(models.Book)
django admin的使用
    1.在应用下注册你的模型表
    
    2.admin url的规律
        http://127.0.0.1:8000/admin/app01/book/              book表的查看
        http://127.0.0.1:8000/admin/app01/book/add/          book表的添加
        http://127.0.0.1:8000/admin/app01/book/3/change/      book表的编辑
        http://127.0.0.1:8000/admin/app01/book/3/delete/      book表的删除页面
        
        http://127.0.0.1:8000/admin/app01/publish/              publish表的查看
        http://127.0.0.1:8000/admin/app01/publish/add/          publish表的添加
        http://127.0.0.1:8000/admin/app01/publish/3/change/      publish表的编辑
        http://127.0.0.1:8000/admin/app01/publish/3/delete/      publish表的删除页面
    
        ps:
            1.admin会给每一个注册了的生成增删改查四条url
        
        
        五大关键性参数的功能
            list_display 控制展示字段 注意不能放多对多字段
    
    配置类的概念
    
    admin启动源码
        django在启动的时候会依次执行每一个应用下的admin.py文件
        from django.utils.module_loading import autodiscover_modules
        autodiscover_modules('admin')
        
    
    源码 单例模式
    
    注册源码
    
    class ModelAdmin(BaseModelAdmin):
        ...
        # 配置类
        
        
    class AdminSite(object):
        def __init__(self, name='admin'):
            self._registry = {}  # model_class class -> admin_class instance
        def register(self, model, admin_class=None, **options):
            """
            Registers the given model(s) with the given admin class.

            The model(s) should be Model classes, not instances.

            If an admin class isn't given, it will use ModelAdmin (the default
            admin options). If keyword arguments are given -- e.g., list_display --
            they'll be applied as options to the admin class.

            If a model is already registered, this will raise AlreadyRegistered.

            If a model is abstract, this will raise ImproperlyConfigured.
            """
            if not admin_class:
                admin_class = ModelAdmin
            # Instantiate the admin class to save in the registry
            self._registry[model] = admin_class(model)
    
    site = AdminSite()
        
    admin.py注册语句
    
    admin.site.register(models.Publish)  # 仅仅是将注册了的模型表和以模型表为参数实例化产生的对象
    # 当做键值对存入了site对象中的_registry字段
    

 

posted on 2019-07-31 22:05  python搬砖狗  阅读(191)  评论(0编辑  收藏  举报