1 #base_admin.py
2 #Django admin 注册功能的形式
3 # sites = {
4 # 'crm':{
5 # 'customers':CustomerAdmin,
6 # 'customerfollowup':CustomerFollowUPAdmin,
7 # }
8 # }
9
10 class AdminRegisterException(Exception): #自定义异常
11 def __init__(self,msg):
12 self.message = msg
13
14 class BaseAdmin(object):#自定义方法
15 list_display = ()
16
17 class AdminSite(object):
18 def __init__(self):
19 self.registered_sites = {} #传到views 里调用
20 def register(self,model,admin_class=None): #默认值None 使用 BaseAdmin
21 app_name = model._meta.app_label#用内置方法获取 APP名字 (crm)
22 model_name = model._meta.model_name#用内置方法获取 表名 (Customer)
23 if app_name not in self.registered_sites:
24 self.registered_sites[app_name] = {} #创建 crm={}
25 if model_name in self.registered_sites[app_name]:
26 raise AdminRegisterException("app [%s] model [%s] has already registered!异常"
27 %(app_name,model_name))#自定义异常,
28 if not admin_class:
29 admin_class = BaseAdmin #默认值None 使用 BaseAdmin
30
31
32
33 # self.registered_sites[app_name][model_name] = admin_class #注册APP
34
35 #registered_sites {'crm': {'customer': <class 'crm.kingadmin.CustomerAdmin'>, 'courserecord': <class 'kingadmin.base_admin.BaseAdmin'>}}
36 #把类名放到class的对象里,然后通过class的对象传到前端
37 admin_class.model = model
38 self.registered_sites[app_name][model_name] = admin_class #注册APP
39
40 site = AdminSite() #实例化类 单例模式
41
42
43 # registered_sites={}
44 # def register(model,admin_class=None): #默认值None 使用 BaseAdmin
45 # app_name = model._meta.app_label#用内置方法获取 APP名字 (crm)
46 # model_name = model._meta.model_name#用内置方法获取 表名 (Customer)
47 # if app_name not in registered_sites:
48 # registered_sites[app_name] = {} #创建 crm={}
49 # if model_name in registered_sites[app_name]:
50 # raise AdminRegisterException("app [%s] model [%s] has already registered!异常"
51 # %(app_name,model_name))#自定义异常
52 # if not admin_class:
53 # admin_class = BaseAdmin #默认值None 使用class BaseAdmin
54 # registered_sites[app_name][model_name] = admin_class #注册APP