1. 程式人生 > >django之admin源碼解析

django之admin源碼解析

源碼 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):pass
def 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源碼解析