當啓動Django時,會執行 setting.py 中的組件django
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'school.apps.SchoolConfig', ]
也就是會掃描admin組件session
from django.contrib import admin
from django.contrib.admin.decorators import register from django.contrib.admin.filters import ( AllValuesFieldListFilter, BooleanFieldListFilter, ChoicesFieldListFilter, DateFieldListFilter, FieldListFilter, ListFilter, RelatedFieldListFilter, RelatedOnlyFieldListFilter, SimpleListFilter, ) from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.options import ( HORIZONTAL, VERTICAL, ModelAdmin, StackedInline, TabularInline, ) from django.contrib.admin.sites import AdminSite, site from django.utils.module_loading import autodiscover_modules __all__ = [ "register", "ACTION_CHECKBOX_NAME", "ModelAdmin", "HORIZONTAL", "VERTICAL", "StackedInline", "TabularInline", "AdminSite", "site", "ListFilter", "SimpleListFilter", "FieldListFilter", "BooleanFieldListFilter", "RelatedFieldListFilter", "ChoicesFieldListFilter", "DateFieldListFilter", "AllValuesFieldListFilter", "RelatedOnlyFieldListFilter", "autodiscover", ] def autodiscover(): autodiscover_modules('admin', register_to=site) #這句話就是掃描每一個組件中的admin.py文件 default_app_config = 'django.contrib.admin.apps.AdminConfig'
能夠看下from django.contrib.admin.sites import AdminSite, site
site = DefaultAdminSite() #單例模式,共用一個對象
總結:app
啓動Django加載admin組件spa
(1)from django.contrib.admin.sites import AdminSite, site 這裏有一個單例對象code
site = DefaultAdminSite()對象
(2)掃描每一個組件下的admin.py blog
autodiscover_modules('admin', register_to=site)
掃描admin.py文件it
from django.contrib import admin class PublishConfig(admin.ModelAdmin): list_display = ['name'] admin.site.register(Publish,PublishConfig) #admin.site就是那個單例模式,因此這些都是公用一個對象 admin.site.register(Author)