CRM系统之stark组件流程分析
CRM系统主要通过自定义stark组件来实现的(参照admin系统自定义):
STARK组件:
1 admin组件
1 如何使用admin
2 admin源码
3 创建自己的admin组件:stark
(1)当django启动时加载每一个app下的stark.py文件 做法:在stark组件的apps.py文件下写下如下代码,这样就会启动每个app下的stark文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.apps import AppConfig from django.utils.module_loading import autodiscover_modules class StarkConfig(AppConfig): name = 'stark' def ready(self): autodiscover_modules('stark')
(2) 注册模型
基于模块单例模式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
func.py: class A(): pass a=A() main.py: from func import a from func import a as b a==b stark组件的单例类: class ModelStark(object): list_display=["__str__"] def __init__(self,model): self.model=model class StarkSite(object): """ stark组件的全局类 """ def __init__(self): self._registry = {} # 1 模型类的注册功能 def register(self, model, admin_class=None): # 设置配置类 admin_class=admin_class or ModelStark self._registry[model] = admin_class(model) site= StarkSite() 在某一个app下的stark.py文件下: from .models import * class BookConfig(ModelStark): list_display=["title"] site.register(Book,BookConfig) site.register(Publish)
(3) 设计URL
语法:url或者path的分发
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 path("test1/",([ 2 path('test02/', ([ 3 path('test04/', views.test04), 4 path('test05/', views.test05), 5 ],None,None)), 6 path('test03/', views.test03), 7 ],None,None)) 8 9 class ModelStark(object): 10 list_display=["__str__"] 11 def __init__(self,model): 12 self.model=model 13 14 def list_view(self,request): 15 16 print(self) 17 print(self.model) 18 print(self.list_display) 19 20 def add_view(self,request): 21 pass 22 23 24 def change_view(self,request,id): 25 pass 26 27 def del_view(self,request): 28 pass 29 30 # url的二级分发 31 def get_urls(self): 32 33 model_name=self.model._meta.model_name 34 app_label=self.model._meta.app_label 35 36 temp = [ 37 38 url(r"^$", self.list_view,name="%s_%s_list"%(app_label,model_name)), 39 url(r"add/", self.add_view,name="%s_%s_add"%(app_label,model_name)), 40 url(r"(\d+)/change/", self.change_view,name="%s_%s_change"%(app_label,model_name)), 41 url(r"(\d+)/delete/", self.del_view,name="%s_%s_delete"%(app_label,model_name)), 42 43 ] 44 45 temp.extend(self.extra_url()) 46 47 return temp 48 49 @property 50 def urls(self): 51 return self.get_urls(), None, None 52 53 class StarkSite(object): 54 """ 55 stark组件的全局类 56 """ 57 def __init__(self): 58 self._registry = {} 59 60 # 1 模型类的注册功能 61 def register(self, model, admin_class=None): 62 # 设置配置类 63 admin_class=admin_class or ModelStark 64 self._registry[model] = admin_class(model) 65 66 67 # 2 动态创建模型类的增删改查URL 68 def get_urls(self): 69 temp = [] 70 for model, config_obj in self._registry.items(): # {Book:BookConfig(Book),Publish:ModelStark(Publish)} 71 72 model_name = model._meta.model_name # "book" 73 app_label = model._meta.app_label # "app01" 74 # url的一级分发 75 temp.append(url(r"%s/%s/" %(app_label,model_name),config_obj.urls)) 76 ''' 77 path("stark/app01/book/",[ 78 url(r"^$", BookConfig(Book).list_view), 79 url(r"add/", BookConfig(Book).add_view), 80 url(r"(\d+)/change/", BookConfig(Book).change_view), 81 url(r"(\d+)/delete/", BookConfig(Book).del_view), 82 ]) 83 84 path("stark/app01/publish/",[ 85 url(r"^$", ModelStark(Publish).list_view), 86 url(r"add/", ModelStark(Publish).add_view), 87 url(r"(\d+)/change/",ModelStark(Publish).change_view), 88 url(r"(\d+)/delete/",ModelStark(Publish).del_view), 89 ]) 90 ''' 91 return temp 92 93 @property 94 def urls(self): 95 return self.get_urls(),None,None 96 97 98 site= StarkSite() 99 100 101 # urls.py(设计URL): 102 path('stark/', site.urls),
补充:有关meta的介绍.可参考官方文档:https://docs.djangoproject.com/zh-hans/2.0/ref/models/options/
中文相关博客可参考:https://www.cnblogs.com/lfxiao/p/8883660.html
(4) 设计增删改查的视图函数
--查看
<1> 展示表头和表体(self.list_display)
<2> 分页
<3> serach
<4> action
<5> filter
--添加,编辑 modelform组件
--删除
业务逻辑