django中admin路由系统工作原理

一、如图所示

from django.contrib import admin
from . import models
class zhangsan(admin.ModelAdmin):
    list_display = ('name', 'email')
    list_editable = ('email',)
    search_fields = ["name"]
    date_hierarchy = 'ctime'
admin.site.register(models.UserInfo,zhangsan)

adango admin源码

site = AdminSite()

def register(self, model_or_iterable, admin_class=None, **options):
if not admin_class:
admin_class = ModelAdmin
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
if model._meta.abstract:
raise ImproperlyConfigured(
'The model %s is abstract, so it cannot be registered with admin.' % model.__name__
)

if model in self._registry:
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
if not model._meta.swapped:
if options:
options['__module__'] = __name__
admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
self._registry[model] = admin_class(model, self)

   1、admin会通过admin.site.register(models.类名,模板类)的方式来注册models中的类。从源码中可以看出,如果我们没有写模板类的话源码就会默认继承ModelAdmin这个模板类,如果我们写了模板类的化也是在继承ModelAdmin这个类的基础上修改模板。

  2、由django admin源码可知site其实是个对象,该对象是个单例模式,而admin.site.register(x,y)实际是admin通过site对象来调用site对象下的register方法,并且还传入了x,y两个参数,其中x指的是models中需要注册的类名,y指代的是注册的类需要使用的模板类。

  3、通过源码 self._registry[model] = admin_class(model, self)就可以看出admin.site.register(x,y)最终得到的结果是个字典,其中字典的k指代的是注册的类,字典的v指代的是模板类实例化产生的对象,而为这个对象传递进去的参数是注册的类

  总结:admin中注册的最终目的是生成一个字典,字典的k是注册的类,v指的是模板类实例化产生的对象,并且为这个对象传递进去的参数是注册的类

二、先注册再路由

  1、为什么会先注册models中的类,再执行路由函数是因为在admin模块中有如下代码:

def autodiscover():
    autodiscover_modules('admin', register_to=site)

   总结:该代码的作用就是告诉django先执行admin.py文件内的代码,注册好类后再执行路由函数

  2、通过模块写一个方法先执行xxx函数,再执行路由函数

from django.apps import AppConfig
class App01Config(AppConfig):
    name = 'app01'
    def ready(self):
        from django.utils.module_loading import autodiscover_modules
        autodiscover_modules('xxx')

   总结:当app程序启动之前就会先找到xxx函数执行里面的代码,然后再执行路由系统

     注释:当app程序启动之前就会先去所有的app中找到xxx函数执行里面的代码,然后执行路由系统

三、admin 路由

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

admin 路由源码
@property
def urls(self):
return self.get_urls(), 'admin', self.name

def get_urls(self):
from django.conf.urls import url, include
from django.contrib.contenttypes import views as contenttype_views
def wrap(view, cacheable=False):
def wrapper(*args, **kwargs):
return self.admin_view(view, cacheable)(*args, **kwargs)
wrapper.admin_site = self
return update_wrapper(wrapper, view)
urlpatterns = [
url(r'^$', wrap(self.index), name='index'),
url(r'^login/$', self.login, name='login'),
url(r'^logout/$', wrap(self.logout), name='logout'),
url(r'^password_change/$', wrap(self.password_change, cacheable=True), name='password_change'),
url(r'^password_change/done/$', wrap(self.password_change_done, cacheable=True),
name='password_change_done'),
url(r'^jsi18n/$', wrap(self.i18n_javascript, cacheable=True), name='jsi18n'),
url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$', wrap(contenttype_views.shortcut),
name='view_on_site'),
]
for model, model_admin in self._registry.items():
urlpatterns += [
url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
]
if model._meta.app_label not in valid_app_labels:
valid_app_labels.append(model._meta.app_label)
if valid_app_labels:
regex = r'^(?P<app_label>' + '|'.join(valid_app_labels) + ')/$'
urlpatterns += [
url(regex, wrap(self.app_index), name='app_list'),
]
return urlpatterns

     1、通过路由系统中的admin.site.urls可知admin路由系统调用的是site对象下的urls方法。而通过源码可知该方法返回了三个值 self.get_urls(), 'admin', self.name,而第一个值执行了个函数,该函数的最终目的的将一个列表返回,而列表内嵌套的是一个个的元组,元组内嵌套的是一个个的url路径。

    2、model.类名._meta.app_label得到的是项目名称,model.类名._meta.model_name得到的是类名称

    3、通过admin的路由系统最终给我们的是个列表内嵌套元组,每个元组内有x,y两个元素,x是生成的对于类的url,y是include()函数,并且向该函数内传递了

model_admin.urls

四、include()函数

  如图所示,include()函数源码

def include(arg, namespace=None, app_name=None):
    if app_name and not namespace:
        raise ValueError('Must specify a namespace if specifying app_name.')
    if app_name:
        warnings.warn(
            'The app_name argument to django.conf.urls.include() is deprecated. '
            'Set the app_name in the included URLconf instead.',
            RemovedInDjango20Warning, stacklevel=2
        )

    if isinstance(arg, tuple):
        # callable returning a namespace hint
        try:
            urlconf_module, app_name = arg
        except ValueError:
            if namespace:
                raise ImproperlyConfigured(
                    'Cannot override the namespace for a dynamic module that provides a namespace'
                )
            warnings.warn(
                'Passing a 3-tuple to django.conf.urls.include() is deprecated. '
                'Pass a 2-tuple containing the list of patterns and app_name, '
                'and provide the namespace argument to include() instead.',
                RemovedInDjango20Warning, stacklevel=2
            )
            urlconf_module, app_name, namespace = arg
    else:
        # No namespace hint - use manually provided namespace
        urlconf_module = arg

    if isinstance(urlconf_module, six.string_types):
        urlconf_module = import_module(urlconf_module)
    patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
    app_name = getattr(urlconf_module, 'app_name', app_name)
    if namespace and not app_name:
        warnings.warn(
            'Specifying a namespace in django.conf.urls.include() without '
            'providing an app_name is deprecated. Set the app_name attribute '
            'in the included module, or pass a 2-tuple containing the list of '
            'patterns and app_name instead.',
            RemovedInDjango20Warning, stacklevel=2
        )

    namespace = namespace or app_name

    # Make sure we can iterate through the patterns (without this, some
    # testcases will break).
    if isinstance(patterns, (list, tuple)):
        for url_pattern in patterns:
            # Test if the LocaleRegexURLResolver is used within the include;
            # this should throw an error since this is not allowed!
            if isinstance(url_pattern, LocaleRegexURLResolver):
                raise ImproperlyConfigured(
                    'Using i18n_patterns in an included URLconf is not allowed.')

    return (urlconf_module, app_name, namespace)

  1、由include()函数源码可知,include函数返回的是个(urlconf_module, app_name, namespace)元组,urlconf_module的本质是个模块,通过这行代码可以看出:urlconf_module = import_module(urlconf_module),

  2、urlconf_module模块的本质其实就是列表内嵌套一行行的url,由此可得知include()函数的作用其实就是路由重分发,而路由重分发得到的数据格式其实就是如下所示:

url(r'^app01/', ([
                        url(r'^userinfo/', ([
                                                url(r'^index/', index,name='idx_xxx_f'),
                                                url(r'^test/', test,name='tst'),
                                            ],'x2','x2')),

                     ],'x1','x1')),

  

  

  

  

posted @ 2017-10-19 00:04  mx轩  阅读(764)  评论(0编辑  收藏  举报