13-crm项目-kingadmin,整张表的只读
项目 13 CRM 只读
2023-09-14 08:59:02 时间
实现整个表的只读
第一步:基类里面加一个入口
class BaseAdmin(object): list_display = [] list_filters = [] search_fields = [] list_per_page = 20 ordering = None filter_horizontal = [] readonly_fields = [] actions = ["delete_selected_objs",] readonly_table = False
.....
第二步:admin类配置:
class CustomerAdmin(BaseAdmin): list_display = ["id",'qq','name','source','consultant','consult_course','date','status','enroll'] list_filters = ['source','consultant','consult_course','status','date'] search_fields = ['qq','name',"consultant__name"] filter_horizontal = ('tags',) #model = models.Customer list_per_page = 2 ordering = "qq" readonly_fields = ["qq","consultant","tags"] #readonly_table = True
第三步:去掉编辑页面的删除和保存按钮
{% if not admin_class.readonly_table %} <div class="form-group"> {% block obj_delete %} <div class="col-sm-2"> <a class="btn btn-danger" href="{% url 'obj_delete' app_name table_name form_obj.instance.id %}">Delete</a> </div> {% endblock %} <div class="col-sm-10 "> <button type="submit" class="btn btn-success pull-right">Save</button> </div> </div> {% endif %}
第四步:把列表页的添加按钮处理一下,
<h3 class="panel-title">{% get_model_name admin_class %} {% if not admin_class.readonly_table %} <a href="{{ request.path }}add/" class="pull-right">Add</a> {% endif %} </h3>
前面的做完了之后都是前端的入口做了,但是还需要后端校验
第五步:后端校验,在动态model中,校验的时候做判断,是否是只读表,
但是这个地方只是校验的新增和修改,还需要做删除的后端判断,
def create_model_form(request,admin_class): .... def default_clean(self): ..... #readonly_table check if admin_class.readonly_table: raise ValidationError( _('Table is readonly,cannot be modified or added'), code='invalid' )
第六步,后端校验,删除的时候判断只读表,
def table_obj_delete(request,app_name,table_name,obj_id): admin_class = king_admin.enabled_admins[app_name][table_name] obj = admin_class.model.objects.get(id=obj_id) if admin_class.readonly_table: errors = {"readonly_table": "table is readonly ,obj [%s] cannot be deleted" % obj} else: errors = {} if request.method == "POST": if not admin_class.readonly_table: obj.delete() return redirect("/king_admin/%s/%s/" %(app_name,table_name)) return render(request,"king_admin/table_obj_delete.html",{"obj":obj, "admin_class":admin_class, "app_name": app_name, "table_name": table_name, "errors":errors })
删除页面展示错误
{% extends 'king_admin/table_index.html' %} {% load tags %} {% block container %} {% display_obj_related objs %} <ul style="color: red"> {% for k,v in errors.items %} <li>{{ k }}:{{ v }}</li> {% endfor %} </ul> <form method="post">{% csrf_token %} <input type="submit" class="btn btn-danger" value="Yes,I'm sure"> <input type="hidden" value="yes" name="delete_confirm"> <input type="hidden" value="{{ selected_ids }}" name="selected_ids"> <input type="hidden" value="{{ action }}" name="action"> <a class="btn btn-info" href="{% url 'table_objs' app_name table_name %}">No,Take me back</a> </form> {% endblock %}
还有地方有删除就是action批量删除的时候所以还要限制
第七步:后端校验,写在基类里面的批量删除,需要做一个处理
def delete_selected_objs(self,request,querysets): app_name = self.model._meta.app_label table_name = self.model._meta.model_name print("--->delete_selected_objs",self,request,querysets) if self.readonly_table: errors = {"readonly_table": "This table is readonly ,cannot be deleted or modified!" } else: errors = {} if request.POST.get("delete_confirm") == "yes": if not self.readonly_table: querysets.delete() return redirect("/king_admin/%s/%s/" % (app_name,table_name)) selected_ids = ','.join([str(i.id) for i in querysets]) return render(request,"king_admin/table_obj_delete.html",{"objs":querysets, "admin_class":self, "app_name": app_name, "table_name": table_name, "selected_ids":selected_ids, "action":request._admin_action, "errors":errors })
相关文章
- [置顶].NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍
- 解决IDEA创建Gradle项目没有src目录问题
- 开源的Eclipse的文件转码插件,可以在不影响中文的情况下改变项目文件编
- 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!
- 登录tomcat服务器首页直接跳转到项目
- 3-crm项目-kingadmin,先实现一个所有表的列表页面,
- maven项目如何生成war文件
- 13-crm项目-kingadmin,整张表的只读
- 【SSH网上商城项目实战13】Struts2实现文件上传功能
- .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍
- 【项目实战】spring-boot-configuration-processor 一个用于生成配置元数据的注解处理器
- 13天搞定python分布式爬虫(视频+项目双管齐下)
- 华为云·云享专家公开课:45分钟掌握Python项目部署与调度核心逻辑直播
- 【阶段三】Python机器学习27篇:机器学习项目实战:数据降维:主成分分析PCA、基本原理与PCA模型:人脸识别
- [手游项目2]-13-mysql代码自动建表
- SQLServer 复制中移除和加入公布而不初始化全部项目
- 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg
- 动作识别0-13:mmaction2(SlowFast)-项目实用代码(1)-demo测试,并解决val无打印,
- 如何应对面试项目经验这一关?阿里老员工来教你