标签:none port dmi 对象 turn height ready settings over
stark组件开发,仿django自带得admin组件。
from django.contrib import admin # Register your models here. from .models import * class UserAdmin(admin.ModelAdmin): list_display = ["pk",‘name‘,‘age‘] list_filter = [‘name‘,‘age‘] # 定制action具体方法 def func(self,request,queryset): queryset.update(age=44) func.short_description = "批量初始化操作" actions = [func] admin.site.register(UserInfo,UserAdmin) admin.site.register(Book)
makemigrations
migrate
createsuperuser (alex 1234qwer)
1. startapp stark
class ModelStark(object): """Book,Userinfo对象""" def __init__(self, model, site): self.model = model self.site = site class StarkSite(object): """site单例对象的创建类""" def __init__(self): self._registry = {} def register(self,model, stark_class=None): if not stark_class: stark_class = ModelStark self._registry[model] = stark_class(model,self) site = StarkSite()
2. settings中配置
from django.apps import AppConfig from django.utils.module_loading import autodiscover_modules class App01Config(AppConfig): name = ‘app01‘ # 程序启动时,扫描app下得指定文件(starkadmin.py)并执行 def ready(self): autodiscover_modules(‘starkadmin‘)
3、创建文件starkadmin:仿admin文件
app01/starkadmin.py
4、url
from django.conf.urls import url from django.shortcuts import HttpResponse class ModelStark(object): """Book,Userinfo对象""" def __init__(self, model, site): self.model = model self.site = site class StarkSite(object): """site单例对象的创建类""" def __init__(self): self._registry = {} def register(self,model, stark_class=None): if not stark_class: stark_class = ModelStark self._registry[model] = stark_class(model,self) def list_view(self,request): return HttpResponse(‘list_view‘) def add(self,request): return HttpResponse(‘add‘) def delete(self, request, id): return HttpResponse(‘delete‘) def change(self, request, id): return HttpResponse(‘change‘) def get_urls2(self): """构造 add/delete/change""" temp = [] temp.append(url(r‘^$‘, self.list_view)) temp.append(url(r‘^add/‘, self.add)) temp.append(url(r‘^(\d+)/delete/‘, self.delete)) temp.append(url(r‘^(\d+)/change/‘, self.change)) return temp @property def urls2(self): return self.get_urls2(), None, None def get_urls(self): """构造urls:app01/book""" temp = [] print(self._registry) for model, stark_class_obj in self._registry.items(): model_name = model._meta.model_name app_label = model._meta.app_label # 分发增删改查 temp.append(url(r‘^%s/%s/‘ % (app_label, model_name), self.urls2)) return temp @property def urls(self): return self.get_urls(), None, None site = StarkSite()
ModelStark()
self.model:用户访问的模型表
from django.conf.urls import url from django.shortcuts import HttpResponse class ModelStark(object): """Book,Userinfo对象""" def __init__(self, model, site): self.model = model self.site = site def list_view(self, request): print(self.model) # <class ‘app01.models.Book‘> 用户访问的模型表 return HttpResponse(‘list_view‘) def add(self, request): return HttpResponse(‘add‘) def delete(self, request, id): return HttpResponse(‘delete‘) def change(self, request, id): return HttpResponse(‘change‘) def get_urls2(self): """构造 add/delete/change""" temp = [] temp.append(url(r‘^$‘, self.list_view)) temp.append(url(r‘^add/‘, self.add)) temp.append(url(r‘^(\d+)/delete/‘, self.delete)) temp.append(url(r‘^(\d+)/change/‘, self.change)) return temp @property def urls2(self): return self.get_urls2(), None, None class StarkSite(object): """site单例对象的创建类""" def __init__(self): self._registry = {} def register(self,model, stark_class=None): if not stark_class: stark_class = ModelStark self._registry[model] = stark_class(model,self) def get_urls(self): """构造urls:app01/book app01/Userinfo""" temp = [] print(self._registry) for model, stark_class_obj in self._registry.items(): model_name = model._meta.model_name app_label = model._meta.app_label print(model,‘---->‘,stark_class_obj) """ <class ‘app01.models.UserInfo‘> ----> <app01.starkadmin.UserConfig object at 0x00000072DDB65198> <class ‘app01.models.Book‘> ----> <stark.service.stark.ModelStark object at 0x00000072DDB65240> """ # 分发增删改查 temp.append(url(r‘^%s/%s/‘ % (app_label, model_name), stark_class_obj.urls2)) """ path(‘app01/userinfo/‘,UserConfig(Userinfo,site).urls2), path(‘app01/book/‘,ModelStark(Book,site).urls2), """ return temp @property def urls(self): return self.get_urls(), None, None site = StarkSite()
标签:none port dmi 对象 turn height ready settings over
原文地址:https://www.cnblogs.com/venicid/p/9490920.html