一、admin的使用
app01的admin.py文件: class BookConfig(admin.ModelAdmin): list_display=[] list_display_links=[] list_filter=[] search_fields=[] def patch(self,request,queryset): pass patch.short_desc="" actions=[patch,] admin.site.register(Book,BookConfig)
二、admin的源码分析
1.启动
加载settings中的install_apps from django.contrib import admin autodiscover_modules('admin')
2.注册
源码:django.contrib.admin.sites模块 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() 每一个app下的admin.py文件: from django.contrib import admin admin.site.register(Book,BookConfig) admin.site.register(Publish)
3.设计urls
1 知识点 1 url(r"index/",views.index) 2 url方法的分发 # 分发 url(r"test/",([ url(r"yuan/",([ url(r"aaa/",aaa), url(r"bbb/",bbb), url(r"ccc/",ccc), ],None,None)), url(r"alex/",alex), ],None,None)) 针对Food表,url: http://127.0.0.1:8000/admin/app02/food/ http://127.0.0.1:8000/admin/app02/food/add/ http://127.0.0.1:8000/admin/app02/food/1/change/ http://127.0.0.1:8000/admin/app02/food/2/delete/ 3 model._meta.model_name :模型表的名称 model._meta.app_label :模型表所在app的名称 config_obj:某个model的配置类(自定义配置类或者默认配置类)对象
三、仿照admin实现一个自定义的增删改查组件
1.随之django的启动,而注册模型
#app01/apps.py from django.apps import AppConfig from django.utils.module_loading import autodiscover_modules class App01Config(AppConfig): name = 'app01' def ready(self): autodiscover_modules('stark')
2. 注册
# app01/stark.py from stark.service.sites import site, ModelStark from app01 import models print('app01................') class BookStark(ModelStark): list_display = ['title', 'price', 'publishDate','publisher'] site.register(models.Book, BookStark) site.register(models.Publish)
3 . 默认配置类ModelStark和全局组件类AdminSite
# stark/service/sites.py from django.conf.urls import url from django.shortcuts import HttpResponse, render class ModelStark(object): list_display = ["__str__"] def __init__(self, model): self.model = model def list_view(self, request): header_list = [] for field in self.list_display: field_obj = self.model._meta.get_field(field) header_list.append(field_obj.verbose_name) print(header_list) new_data_list = [] obj_list = self.model.objects.all() for obj in obj_list: temp = [] for field in self.list_display: val = getattr(obj, field) temp.append(val) new_data_list.append(temp) return render(request, 'list_view.html', locals()) def add_view(self, request): return HttpResponse('add_view') def change_view(self, request, pk): return HttpResponse('change_view') def del_view(self, request, pk): return HttpResponse('del_view') def get_urls(self): tmp = [] tmp.append(url(r'^$', self.list_view)) tmp.append(url(r'add/$', self.add_view)) tmp.append(url(r'(d+)/delete/$', self.del_view)) tmp.append(url(r'(d+)/change/$', self.change_view)) return tmp, None, None @property def urls(self): return self.get_urls() class AdminSite(object): def __init__(self): self._registy = {} def register(self, model, stark_class=None): if not stark_class: stark_class = ModelStark self._registy[model] = stark_class(model) def get_urls(self): tmp = [] for model, config_obj in self._registy.items(): tmp.append(url(r"{}/{}/".format(model._meta.app_label, model._meta.model_name), config_obj.urls)) return tmp, None, None @property def urls(self): return self.get_urls() site = AdminSite()