一、背景
CRM项目重新总结一下,重写之前的项目发现有不少知识点已经忘记,所以特此来重新总结一下一便后续能够回忆起
二、代码分析
1. 核心代码
from django.conf.urls import url from django.shortcuts import HttpResponse,render class VbcConfig(object): list_display = [] def __init__(self,model_class,site): self.model_class = model_class self.site = site def get_urls(self): app_model_name = (self.model_class._meta.app_label,self.model_class._meta.model_name,) url_pattern = [ url(r‘^$‘,self.change_list_view,name=‘%s_%s_change_list‘%app_model_name), url(r‘^add/$‘,self.add_view,name=‘%s_%s_add‘%app_model_name), url(r‘^(\d+)/change/$‘,self.change_view,name=‘%s_%s_change‘%app_model_name), url(r‘^(\d+)/delete/$‘,self.delete_view,name=‘%s_%s_delete‘%app_model_name), ] return url_pattern @property def urls(self): return self.get_urls() # ########################### 处理请求的方法 ############# def change_list_view(self,request,*args,**kwargs): # 处理表头 head_list = [] for field_name in self.list_display: if isinstance(field_name,str): verbose_name= self.model_class._meta.get_field(field_name).verbose_name else: verbose_name = field_name(self,is_header=True) head_list.append(verbose_name) # 处理数据 data_list = self.model_class.objects.all() new_data_list = [] for row in data_list: temp =[] for field_name in self.list_display: if isinstance(field_name,str): val = getattr(row,field_name) else: val = field_name(self,row) temp.append(val) new_data_list.append(temp) return render(request,‘changelist.html‘,{‘new_data_list‘:new_data_list,‘head_list‘:head_list}) def add_view(self,request,*args,**kwargs): return HttpResponse(‘添加‘) def change_view(self,request,*args,**kwargs): return HttpResponse(‘修改‘) def delete_view(self,request,*args,**kwargs): return HttpResponse(‘删除‘) class VbcSite(object): def __init__(self): self._registry = {} def registry(self,model_class,vbc_config_class=None): if not vbc_config_class: vbc_config_class = VbcConfig # 如果没有自定义Config类则实例化VbcConfig类 self._registry[model_class] = vbc_config_class(model_class,self) # 将数据库model传入进来进行实例化 def get_urls(self): url_pattern = [] for model_class,vbc_config_obj in self._registry.items(): print(model_class,vbc_config_obj) # 为每一个类,创建4个url """ /stark/app01/userinfo/ /stark/app01/userinfo/add /stark/app01/userinfo/(\d+)/change/ /stark/app01/userinfo/(\d+)/delete/ """ app_name = model_class._meta.app_label model_name = model_class._meta.model_name curd_url = url(r‘%s/%s/‘%(app_name,model_name),(vbc_config_obj.urls,None,None)) url_pattern.append(curd_url) return url_pattern @property def urls(self): return (self.get_urls(),None,‘vbc‘) site = VbcSite()
2. 实例化model类
from django.shortcuts import HttpResponse from django.utils.safestring import mark_safe from stark.service import v1 from app01 import models class UserInfoConfig(v1.VbcConfig): def checkbox(self,obj=None,is_header=False): if is_header: return ‘序号‘ return mark_safe("<input type=‘checkbox‘ name=‘pk‘ value=‘%s‘></>"%obj.id) def edit(self,obj=None,is_header=False): if is_header: return ‘操作‘ return mark_safe(‘<a href="/edit/%s">编辑</a>‘%obj.id) def delete(self,obj=None,is_header=False): if is_header: return ‘表头‘ return mark_safe(‘<a href="/edit/%s">删除</a>‘%obj.id) list_display = [checkbox,‘id‘,‘name‘,edit,delete] class RoleConfig(v1.VbcConfig): list_display = [‘id‘,‘title‘] class UserTypefig(v1.VbcConfig): list_display = [‘id‘,‘title‘] v1.site.registry(models.UserInfo,UserInfoConfig) v1.site.registry(models.UserType,UserTypefig) v1.site.registry(models.Role,RoleConfig)
三. 学习的知识点:如何从model中获取字段名称
1. model
from django.db import models # Create your models here. class UserInfo(models.Model): name = models.CharField(max_length=32,verbose_name=‘用户名‘) def __str__(self): return self.name class Role(models.Model): title = models.CharField(max_length=32,verbose_name=‘用户角色‘) users = models.ForeignKey(to=‘UserInfo‘,verbose_name=‘用户职位‘,on_delete=models.CASCADE) def __str__(self): return self.title class UserType(models.Model): title = models.CharField(max_length=32,verbose_name=‘用户类型‘) def __str__(self): return self.title
2. 代码
# ########################### 处理请求的方法 ############# def change_list_view(self,request,*args,**kwargs): # 处理表头 head_list = [] for field_name in self.list_display: if isinstance(field_name,str): verbose_name= self.model_class._meta.get_field(field_name).verbose_name # 在_meta中有个get_field方法,通过将model字段名称传入进去,最后执行.verbose_name就可以获取model字段名称 else: verbose_name = field_name(self,is_header=True) head_list.append(verbose_name) # 处理数据 data_list = self.model_class.objects.all() new_data_list = [] for row in data_list: temp =[] for field_name in self.list_display: if isinstance(field_name,str): val = getattr(row,field_name) else: val = field_name(self,row) temp.append(val) new_data_list.append(temp) return render(request,‘changelist.html‘,{‘new_data_list‘:new_data_list,‘head_list‘:head_list})
小结:
self.model_class._meta.get_field(field_name).verbose_name # 在_meta中有个get_field方法,通过将model字段名称传入进去,最后执行.verbose_name就可以获取model字段名称
原文地址:https://www.cnblogs.com/supery007/p/8638377.html
时间: 2024-10-03 14:44:58