<!--done--> 知識預覽 Django 如何使用admin組件來對後臺數據進行管理的? Django admin如何實現後臺數據管理的?(admin源碼解析) 如何仿照admin實現一個自定義的增刪改查的組件? 回到頂部 Django 如何使用admin組件來對後臺數據進行管理的? 在每個a ...
知識預覽
回到頂部Django 如何使用admin組件來對後臺數據進行管理的?
在每個app下的admin.py文件中進行註冊: from app名.models import 模型類名 from django.contrib import admin admin.site.register(模型類名) class 自定義配置類名(admin.ModelAdmin): list_display = ["欄位名1","欄位名1"] admin.site.register(模型類名,自定義配置類名)
回到頂部
Django admin如何實現後臺數據管理的?(admin源碼解析)
一、啟動 django啟動後,會載入settings中的INSTALLED_APPS from django.contrib import admin 在__init.py中: from django.contrib.admin.sites import AdminSite, site // 導入模塊的實例化對象,此為單例模式 from django.utils.module_loading import autodiscover_modules def autodiscover(): autodiscover_modules('admin', register_to=site) //載入每一個app下的admin.py文件 # 優先執行哪個app下的admin.py 是由settings中的INSTALLED_APPS的載入順序決定的
二、註冊 admin.site.register(Author) class BookConfig(admin.ModelAdmin): pass admin.site.register(Book,BookConfig) admin.py---sites.py中: class AdminSite(object): def __init__(self): self._registry = {} def register(self,model,admin_class=None): if not admin_class: admin_class = ModelAdmin self._registry[model] = admin_class(model, self) site = AdminSite() admin.site._registry --> { <class 'django.contrib.auth.models.Group'>:<django.contrib.auth.admin.GroupAdmin object at 0x0000000003E70DD8>, <class 'django.contrib.auth.models.User'>:<django.contrib.auth.admin.UserAdmin object at 0x0000000003E9DCC0>, <class 'app01.models.Author'>:ModelAdmin(Author), <class 'app01.models.AuthorDetail'>:ModelAdmin(AuthorDetail), <class 'app01.models.Book'>: BookConfig(Book), <class 'app01.models.Publish'>:PublishConfig(Publish), } 在app下admin.py文件中註冊,admin.site._registry中才會有此鍵值對。
三、設計url ##### 一級url分發 ##### urlpatterns = [ url(r"^my_admin/",([ url(r"^book/",app01_views.list_book), url(r"^publish/",app01_views.list_publish), url(r"^food/",app02_views.list_food), ],None,None)) ] ##### 二級url分發 ##### urlpatterns = [ url(r"^my_admin/",([ url(r"^book/",([ url(r"^$",app01_views.list_book), url(r"^add/$",app01_views.add_book), url(r"^(\d+)/change/$",app01_views.edit_book), url(r"^(\d+)/delete/$",app01_views.del_book), ],None,None)), url(r"^publish/",app01_views.list_publish), url(r"^food/",app02_views.list_food), ],None,None)) ] 在urls.py中: from django.conf.urls import url from django.contrib import admin from django.shortcuts import HttpResponse def listview(request): return HttpResponse("listview") def addview(request): return HttpResponse("addview") def changeview(request,id): return HttpResponse("changeview") def deleteview(request,id): return HttpResponse("deleteview") def get_urls_02(): res = [ url(r"^$",listview), url(r"^add/$",addview), url(r"^(\d+)/change/$",changeview), url(r"^(\d+)/delete/$",deleteview), ] return res def get_urls_01(): res = [] print("urls.py文件中admin.site._registry -->",admin.site._registry) """ urls.py文件中admin.site._registry --> { <class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x0000000003E70E10>, <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x0000000003E9DCF8>, <class 'app01.models.Author'>: <django.contrib.admin.options.ModelAdmin object at 0x0000000003EB0940>, <class 'app01.models.AuthorDetail'>: <django.contrib.admin.options.ModelAdmin object at 0x0000000003EB0978>, <class 'app01.models.Book'>: <app01.admin.BookConfig object at 0x0000000003EB09B0>, <class 'app01.models.Publish'>: <app01.admin.PublishConfig object at 0x0000000003EB09E8>, <class 'app02.models.Food'>: <django.contrib.admin.options.ModelAdmin object at 0x0000000003EB07F0> } """ for model,config_obj in admin.site._registry.items(): print("模型類變數model-->",model) print("配置類對象config_obj-->",config_obj) model_name = model._meta.model_name # 獲取字元串形式的模型類名 app_label = model._meta.app_label # 獲取字元串形式的app名 print("model_name-->{},type(model_name)-->{}".format(model_name,type(model_name))) print("app_label-->{},type(model_name)-->{}".format(app_label,type(app_label))) """ 模型類變數model--> <class 'django.contrib.auth.models.Group'> 配置類對象config_obj--> auth.GroupAdmin model_name-->group,type(model_name)--><class 'str'> app_label-->auth,type(model_name)--><class 'str'> 模型類變數model--> <class 'django.contrib.auth.models.User'> 配置類對象config_obj--> auth.UserAdmin model_name-->user,type(model_name)--><class 'str'> app_label-->auth,type(model_name)--><class 'str'> 模型類變數model--> <class 'app01.models.Author'> 配置類對象config_obj--> app01.ModelAdmin model_name-->author,type(model_name)--><class 'str'> app_label-->app01,type(model_name)--><class 'str'> 模型類變數model--> <class 'app01.models.AuthorDetail'> 配置類對象config_obj--> app01.ModelAdmin model_name-->authordetail,type(model_name)--><class 'str'> app_label-->app01,type(model_name)--><class 'str'> 模型類變數model--> <class 'app01.models.Book'> 配置類對象config_obj--> app01.BookConfig model_name-->book,type(model_name)--><class 'str'> app_label-->app01,type(model_name)--><class 'str'> 模型類變數model--> <class 'app01.models.Publish'> 配置類對象config_obj--> app01.PublishConfig model_name-->publish,type(model_name)--><class 'str'> app_label-->app01,type(model_name)--><class 'str'> 模型類變數model--> <class 'app02.models.Food'> 配置類對象config_obj--> app02.ModelAdmin model_name-->food,type(model_name)--><class 'str'> app_label-->app02,type(model_name)--><class 'str'> """ add_url = url(r"^{}/{}/".format(app_label,model_name),(get_urls_02(),None,None)) res.append(add_url) return res urlpatterns = [ url(r"^my_admin/",(get_urls_01(),None,None)), ]
回到頂部
如何仿照admin實現一個自定義的增刪改查的組件?
一、啟動 1、創建一個與Django項目無關的,可以單獨分離出來用在多個項目上的名稱為my_admin的app: python manage.py startapp my_admin 2、創建兩個與Django項目有關的兩個app: python manage.py startapp app01 python manage.py startapp app02 3、在settings.py中的INSTALLED_APPS變數中添加: 'app01.apps.App01Config', 'app02.apps.App02Config', 'my_admin.apps.MyAdminConfig', 4、將my_admin、app01和app02中的admin.py文件全部刪除,重新分別在app01和app02中添加myAdmin.py 5、app01下models.py中添加Book,Publish,AuthorDetail,Author類 6、app02下models.py中添加Food類 7、遷移資料庫: python manage.py makemigrations python manage.py migrate 8、my_admin的app下有一個apps.py文件,在此文件中添加: from django.utils.module_loading import autodiscover_modules class MyAdminConfig(AppConfig): name = 'my_admin' def ready(self): autodiscover_modules("myAdmin")
二、註冊 1、my_admin的app下創建一個python package的包,名稱為service 2、在service文件夾下新建一個sites.py文件 3、sites.py中添加以下代碼: class ModelMyAdmin(): list_display = [] def __init__(self,model): self.model = model class MyAdminSite(): def __init__(self): self._registry = {} def register(self,model,my_admin_class = None): if not my_admin_class: my_admin_class = ModelMyAdmin self._registry[model] = my_admin_class(model) site = MyAdminSite() 4、在app01下的myAdmin.py中註冊模型類: from my_admin.service.sites import ModelMyAdmin,site from app01.models import Book,Publish,Author,AuthorDetail class BookConfig(ModelMyAdmin): list_display = ["title","publish_date","price"] site.register(Book,BookConfig) site.register(Publish) site.register(Author) site.register(AuthorDetail) print("app01下的site._registry-->",site._registry) 啟動項目後,列印出此字典證明已經註冊成功 { <class 'app01.models.Author'>: <my_admin.service.sites.ModelMyAdmin object at 0x0000000003EA70B8>, <class 'app01.models.AuthorDetail'>: <my_admin.service.sites.ModelMyAdmin object at 0x0000000003EB1B00>, <class 'app01.models.Book'>: <app01.myAdmin.BookConfig object at 0x0000000003EB1EB8>, <class 'app01.models.Publish'>: <my_admin.service.sites.ModelMyAdmin object at 0x0000000003EB1EF0> }
三、設計url 1、在urls.py文件中: from django.conf.urls import url from my_admin.service.sites import site urlpatterns = [ url(r'^my_admin/',site.urls), ] 2、在sites.py文件中的MyAdminSite類中繼續添加一個urls方法: from django.conf.urls import url def get_urls_01(self): res = [] for model,config_obj in self._registry.items(): model_name = model._meta.model_name app_label = model._meta.app_label add_url = url(r'{}/{}/'.format(app_label,model_name),config_obj.urls) #config_obj:某個model的配置類(自定義配置類或者預設配置類)對象 res.append(add_url) return res @property def urls(self): return self.get_urls_01(),None,None 3、在sites.py文件中的ModelMyAdmin類中繼續添加一個urls方法: from django.shortcuts import render def listview(self,request): print("self-->",self) # 當前訪問模型類的配置類對象 print("self.model-->",self.model) # 當前訪問模型類 data = self.model.objects.all() return render(request,"listview.html",{"data_list":data}) def addview(self,request): return HttpResponse("addview") def changeview(self,request, id): return HttpResponse("changeview") def deleteview(self,request, id): return HttpResponse("deleteview") def get_urls_02(self): res = [ url(r'^$',self.listview) url(r'^add/$',self.addview) url(r'^(\d+)/change/$',self.changeview) url(r'^(\d+)/delete/$',self.deleteview ] return res @property def urls(self): return self.get_urls_02(),None,None
為什麼要將get_urls_02的方法寫入到ModelMyAdmin類中,而不寫在MyAdminSite類中?
將get_urls_02寫入到MyAdminSite類中,由於單例模式造成返回的是同一個頁面,如果是簡單的返回一個HttpResponse對象,是可以的;
但是現實需求是不同的表要展示不同的視圖數據而且不同的表要有不同的配置信息,故需要寫入在ModelMyAdmin類中。