django之admin源碼解析
阿新 • • 發佈:2019-04-11
源碼 size pass code all 分享圖片 -s http reg
解析admin的源碼
第一步:項目啟動,加載settings文件中的 INSTALLED_APPS
裏邊有幾個app就加載幾個,按照註冊順序來執行。
第二步:其中加載的是admin.py,加載每一個app下的admin.py文件
第三步:執行代碼
第四步:看admin.site走的流程
咱走一下源碼
總結一下:
第五步:執行register方法
admin.site.register(Book, BookAdmin)
admin.site.register(Publish)
class ModelAdmin(BaseModelAdmin):passdef register(self, model_or_iterable, admin_class=None, **options): if not admin_class: admin_class = ModelAdmin # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
註冊就結束了!
補充一下:
在每一個app的admin .py中加上
print(admin.site._registry) # 執行結果?
app01:
app02:
django之admin源碼解析