码迷,mamicode.com
首页 > 其他好文 > 详细

(2) 自定义admin

时间:2018-07-31 21:34:48      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:play   redirect   profile   red   arc   rect   pos   short   print   

from django.shortcuts import  render,redirect
from crm import models

enabled_admins = {}

class BaseAdmin(object):
    list_display = []
    list_filters = []
    search_fields = []
    list_per_page = 20
    ordering = None
    filter_horizontal = []
    readonly_field = []
    actions = ["delete_selected_objs",]

    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 request.POST.get("delete_confirm") == "yes":
            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
                                                              })


class CustomerAdmin(BaseAdmin):
    list_display = ["id",qq,name,source,consultant,consult_course,date,status]
    list_filters = [source,consultant,consult_course,status,date]
    search_fields = [qq,name,"consultant__name"]
    filter_horizontal = (tags,)
    #model = models.Customer
    list_per_page = 5
    ordering = "qq"
    readonly_field = ["qq","consultant"]

    def clean_name(self):
        print("name clean validation",self)


    actions = ["delete_selected_objs","test"]
    def test(self,request,querysets):
        print("in test",)
    test.display_name  = "测试动作"

#要将models中的对象与定制的对象关联起来
class CustomerFollowUpAdmin(BaseAdmin):
    list_display = (customer,consultant,date)

class UserProfileAdmin(BaseAdmin):
    list_display = (user,name)

def register(model_class,admin_class=None):
    if model_class._meta.app_label not in enabled_admins:
        enabled_admins[model_class._meta.app_label] = {} #enabled_admins[‘crm‘] = {}
    #admin_obj = admin_class()
    admin_class.model = model_class #绑定model 对象和定制对象admin类,在前端就是渲染不出来
    enabled_admins[model_class._meta.app_label][model_class._meta.model_name] = admin_class
    #enabled_admins[‘crm‘][‘customerfollowup‘] = CustomerFollowUpAdmin


register(models.Customer,CustomerAdmin)
register(models.CustomerFollowUp,CustomerFollowUpAdmin)
register(models.UserProfile,UserProfileAdmin)

_meta.app_label:找到表中类的APP名称

_meta.model_name:找到表中类的小写名称

admin_class.model = model_class:将定制的admin与它的类绑定起来

 

(2) 自定义admin

标签:play   redirect   profile   red   arc   rect   pos   short   print   

原文地址:https://www.cnblogs.com/chengtianyu/p/9397656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!