admin组件详解
先根据admin组件启动流程复习下django项目启动至请求过来发生的事
流程
知识点
def ready(self):
autodiscover_modules('stark')
class StarkSite(object):
def __init__(self, name='admin'):
self._registry = {} #记录注册的model类
def register(self, model, admin_class=None, **options):
if not admin_class:
admin_class = ModelStark # 配置类
self._registry[model] = admin_class(model)
#一级路由代码
def get_urls(self): temp = [] for model_class, config_obj in self._registry.items():
print("===>", model_class, config_obj) model_name = model_class._meta.model_name
app_label = model_class._meta.app_label
print("===>", app_label, model_name) temp.append(url(r'^%s/%s/' % (app_label, model_name),config_obj.urls))
def get_urls(self): temp=[
url("^$",self.list_view,name="%s_%s_list"%(self.app_model_name)),
url("^add/$",self.add_view,name="%s_%s_add"%(self.app_model_name)),
url("^(\d+)/change/$",self.change_view,name="%s_%s_change"% (self.app_model_name)),
url("^(\d+)/delete/$",self.delete_view,name="%s_%s_delete"%(self.app_model_name)), ] temp.extend(self.extra_urls()) return temp @property #此装饰器的作用是让方法变成可以像属性一样调用
def urls(self):
return self.get_urls(),None,None
流程图
>
至于增删改查视图中的方法就步过介绍了,自定义admin组件的基本逻辑就是这样