13-crm项目-kingadmin,整张表的只读

实现整个表的只读

第一步:基类里面加一个入口

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
                                                              })

 

posted @ 2020-09-24 10:04  技术改变命运Andy  阅读(120)  评论(0编辑  收藏  举报