一.在app中创建python文件(例如:kings.py)
如图:生成的数据为{"app名":{"表名"userprofileadmin类},实际上就是admin.py中的admin.site.register(models.UserProfile,UserProfileAdmin)注册
from app01 import models #{"app01":{"userprofile"userprofileadmin类} enabled_admins={} #1.创建基类 class BaseAdmin(object): list_display=[] list_filter=[] class UserPorfileAdmin(BaseAdmin): list_display = ["name","user_id"] #model=models.Customer AAA class RoleAdmin(BaseAdmin): list_display = ["name"] ‘‘‘ register函数相当于admin.site.register() models_class._meta.app_label app名字 models_class._meta.model_name 表名 ‘‘‘ def register(models_class,admin_class=None): #models_class是表名,admin_class相当于app01.admin中的UserProfileAdmin if models_class._meta.app_label not in enabled_admins: enabled_admins[models_class._meta.app_label]={} admin_class.model=models_class #相当于上AAA===将CustomerAdmin和Customer表关联 enabled_admins[models_class._meta.app_label][models_class._meta.model_name]=admin_class register(models.UserProfile,UserPorfileAdmin) register(models.Role,RoleAdmin)
原文:https://www.cnblogs.com/gaoyukun/p/9222025.html