• CRUD组件的高阶使用


    1.list页面自定列显示:
    class PermissionConfig(sites.AryaConfig):
     
        def dabo(self, obj=None, is_header=False):
            if is_header:
                return "大波"
            else:
                return "大波-"+obj.caption
        list_display = ["caption", "url", dabo,"menu"]
     
    sites.site.register(models.Permission, PermissionConfig)
     
    2.功能界面更改:重写add_view使其界面展示url list---->
    获取url_list
    def get_all_url(patterns, prev, is_first=False, result=[]):
        """
        [
            (ur1,ur1,)
            (ur1,ur1,)
            (ur1,ur1,)
            (ur1,ur1,)
        ]
        :param patterns:
        :param prev:
        :param is_first:
        :param result:
        :return:
        """
        if is_first:
            result.clear()
        for item in patterns:
            v = item._regex.strip("^$")
            if isinstance(item, RegexURLPattern):
                val = prev + v
                result.append((val, val,))
            else:
                get_all_url(item.urlconf_name, prev + v)
    
        return result
    View Code
    同时在permission_add.html和permission_add_popup.html适当位置添加如下代码用以渲染
    <div>
        <h3>url-list</h3>
        <ul>
            {% for url in url_list %}
                <li>{{ url.0 }}</li>
            {% endfor %}
        </ul>
     
    </div>
    3.重写ModelForm深度定制功能页:编写PermissionModelForm以完成url以choice方式实现.
    注意事项:
    class PermissionModelForm(ModelForm):
        url=fields.ChoiceField() #若该字段名和Permission中的某字段重名,则会将该字段替换,不重名,则添加新的
        #from pro_crm.urls import urlpatterns
        #url.choices=get_all_url(urlpatterns,"/",True) #此种赋值方式1:会使urlpatterns提前执行,引发错误
                                                      #2.当新增url,不能时时刷新,因为PermissionModelForm声明时会取一遍url,
                                                      #当用其new对象时,不会再去获取新的url,因此需要将获取url的逻辑写在__init__
    实现代码:
    class PermissionModelForm(ModelForm):
        url = fields.ChoiceField()  # 若该字段名和Permission中的某字段重名,则会将该字段替换,不重名,则添加新的
    
        # from pro_crm.urls import urlpatterns
        # url.choices=get_all_url(urlpatterns,"/",True) #此种赋值方式1:会使urlpatterns提前执行,引发错误
        # 2.当新增url时,不能时时刷新,因为PermissionModelForm声明时会取一遍url,
        # 当用其new对象时,不会再去获取新的url,因此需要将获取url的逻辑写在__init__中
    
    
        class Meta:
            model = models.Permission
            fields = "__all__"
    
        def __init__(self, *args, **kwargs):
            super(PermissionModelForm, self).__init__(*args, **kwargs)
            from pro_crm.urls import urlpatterns
            self.fields["url"].choices = get_all_url(urlpatterns, "/", True)
    View Code

    4.list页面foreignKey和ManyToMany字段的显示,本质上是list_display列表中添加函数,在函数中控制显示逻辑choices
    code:
    class ClassListConfig(sites.AryaConfig):
    
        def course_display(self, obj=None, is_header=False):
            if is_header:
                return "班级"
            else:
                tpl = "{}({}期)".format(obj.course.name,obj.semester)
                return tpl
    
    
        def teachers_display(self,obj=None,is_header=False):
            if is_header:
                return "任课老师"
            else:
                teachers = obj.teachers.all()
                result = []
                for teacher in teachers:
                    tpl = "<span>{}</span>".format(teacher.name)
                    result.append(tpl)
                return mark_safe(" ".join(result))
    
        list_display = ["school",course_display,"tutor",teachers_display,"price",]
    
    sites.site.register(models.ClassList,ClassListConfig)
    View Code

    5.list页面choices的显示:
    class CustomerConfig(sites.AryaConfig):
        ....
        def gender_display(self,obj=None,is_header=False):
            if is_header:
                return "性别"
            else:
                return obj.get_gender_display()
     
        list_display = ["name",gender_display,"qq","consultant",courses_display]
    sites.site.register(models.Customer,CustomerConfig)

     
     
     6.date的显示
        def date(self, obj=None, is_header=False):
            if is_header:
                return "交款日期"
            else:
                return obj.date.strftime("%Y-%m-%d")
    
        list_display = ["customer","class_list",pay_type_choices,"paid_fee","consultant",date]

     7.list_filter的配置

    class CustomerConfig(sites.AryaConfig):
        
        .......
    
        list_filter = [
            # sites.FilterOption("name",False,lambda x:x.name,lambda x:x.name),#这种配置无意义
            sites.FilterOption("consultant",False,condition=Q(depart=1)),#传条件以显示筛选项
            sites.FilterOption("course",True),#咨询课程
            sites.FilterOption("gender",False),#choices
    
        ]
    sites.site.register(models.Customer,CustomerConfig)

  • 相关阅读:
    实现可重启线程
    让别人能登陆你的mysql
    zmq消息订阅
    git备忘
    【LeetCode】数组排列问题(permutations)(附加next_permutation解析)
    【LeetCode】 数相加组合 Combination Sum
    【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array
    【LeetCode】【数组归并】Merge k Sorted Lists
    【LeetCode】【动态规划】Generate Parentheses(括号匹配问题)
    【Leetcode】Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/jec1999/p/7747620.html
Copyright © 2020-2023  润新知