django之admin流程
admin
类复习: class Base(object): def __init__(self,val): self.val = val def func(self): self.test() print(self.val) def test(self): print('Base.test') class Foo(Base): def test(self): print('Foo.test') class Bar(object): def __init__(self): self._registry = {} def regiter(self,a,b=None): if not b: b = Base self._registry[a] = b(a) # 函数、类、对象 obj = Bar() obj.regiter(1,Foo) obj.regiter(2) obj._registry[1].func() # Foo的对象 obj._registry[2].func() # Base对象 - admin 路由规则: /admin/app01/role/ 查看角色列表 /admin/app01/role/add/ 添加角色 /admin/app01/role/2/change/ 编辑 /admin/app01/role/2/delete/ 删除 /admin/app01/userinfo/ /admin/app01/userinfo/add/ /admin/app01/userinfo/1/change/ /admin/app01/userinfo/1/delete/ - Django启动 - admin流程 - 执行已经注册app的admins.py文件 - 创建AdminSite类的admin.site对象 执行对象register()方法, 把数据赋值到self.registry={}中 - self._registry[model] = ModelAdmin(model, self) - 进入urls.py 执行admin.site.urls方法 - 返回一个元组 元组的第一个元素是列表 - 所以生成url的时候经历了两个类 一个是admin.site类 admin.site会调用model_admin类中的urls - admin源码流程 a. 运行程序,找到每一个app中的 admin.py 文件,并加载 - app01.admin.py - 创建admin.site中的对象 - 执行对象的 register方法,目的:讲注册类添加到 _registry中 _registry = { models.Role: ModelAdmin(models.Role,admin.site), models.UserInfo: ModelAdmin(models.UserInfo,admin.site) models.UserType: ModelAdmin(models.UserType,admin.site) } - app02.admin.py - 用app01.admin中创建那个admin.site对象 - 执行对象的 register方法,目的:讲注册类添加到 _registry中 _registry = { models.Role: ModelAdmin(models.Role,admin.site), models.UserInfo: ModelAdmin(models.UserInfo,admin.site) models.UserType: ModelAdmin(models.UserType,admin.site) models.Article: ModelAdmin(models.Article,admin.site) } admin.site是一个对象(单例模式创建),其中封装了: _registry = { models.Role: ModelAdmin(models.Role,admin.site), models.UserInfo: ModelAdmin(models.UserInfo,admin.site) models.UserType: ModelAdmin(models.UserType,admin.site) models.Article: ModelAdmin(models.Article,admin.site) } b. urls.py 再次调用 admin.site 对象的 urls属性: urlpatterns = [ url(r'^admin/', admin.site.urls), ] class ModelAdmin(object): def __init__(self,model_class,site): self.model_class = model_class self.site = site def changelist_view(self,request): data_list = self.model_class.objects.all() return HttpResponse('列表页面') def add_view(self,request): return HttpResponse('添加页面') def delete_view(self,request,nid): return HttpResponse('删除页面') def change_view(self,request,nid): return HttpResponse('修改页面') def get_urls(self): urlpatterns = [ url(r'^$', self.changelist_view), url(r'^add/$', self.add_view), url(r'^(.+)/delete/$', self.delete_view), url(r'^(.+)/change/$', self.change_view), ] return urlpatterns @property def urls(self): return self.get_urls() class AdminSite(object): def __init__(self): self._registry = {} def register(self,model_class,model_admin): self._registry[model_class] = model_admin(model_class,self) def get_urls(self): """ models.Role: ModelAdmin(models.Role,admin.site), models.UserInfo: ModelAdmin(models.UserInfo,admin.site) models.UserType: ModelAdmin(models.UserType,admin.site) models.Article: ModelAdmin(models.Article,admin.site) """ url_list = [] for model_class,model_admin in self._registry.items(): app_name = model_class._meta.app_label model_name = model_class._meta.model_name url_list += [ url('%s/%s' %(app_name,model_name,), include(model_admin.urls)) ] return url_list @property def urls(self): return (self.get_urls(), None,None )