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

CRM手记-4 | Django

时间:2018-01-13 18:56:11      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:class   ioc   gpo   imp   span   默认值   uil   tom   model   

# 1.过滤功能:
#    多条件过滤;url上表示:?source=1&consultant_id=2
# 做成下拉菜单的形式;
    # table_obj_list.html
        <div>
            <form>
                {% for filter_column in admin_class.list_filter %}
                
                    {% build_filter_ele filter_column admin_class%}
                    
                {% endfor %}
                
            </form>
        </div>
    # kingadmin_tags.py
        ...
        @register.simple_tag
        def build_filter_ele(filter_column,admin_class):
            filter_ele = "<select>"
    >> a = models.CustomerInfo._meta.get_field(status) # 取到指定类型的字段;
    >> dir(a)
    >> [... choices,flatchoice...]
    >> a.choices
    ((0,未报名),(1,已报名),(2,已退学)        
    # a.chioces已经是一个序列了,直接循环|先不考虑没有choices的;
    # kingadmin_tags.py
        ...
        @register.simple_tag
        def build_filter_ele(filter_column,admin_class):
            filter_ele = "<select>"
            column_obj = admin_class.model._meta.get_field(filter_column)
            for choice in column_obj.get_chioces():
                option = "<option value=‘%s‘>%s</option>"%chioce
                filter_ele += option
            filter_ele += "</select>"
            return filter_ele
    # 问题:有些字段是外键,或者没有choice,就会报错;
    # 措施:django中有._meta.get_chioces()有一个空值键的默认值;
    >> a.get_choices()
    ((‘‘.-------),(0,未报名),(1,已报名),(2,已退学)
    # 没有choice会报一个AttributeError的错误;
    # 措施:抓住异常,先让程序执行,再去处理;
        # kingadmin_tags.py
            ...
            @register.simple_tag
            def build_filter_ele(filter_column,admin_class):
                filter_ele = "<select>"
                column_obj = admin_class.model._meta.get_field(filter_column)
                try:
                    for choice in column_obj.get_chioces():
                        option = "<option value=‘%s‘>%s</option>"%chioce
                        filter_ele += option
                except AttributeError as e:
                    print("err",e)
                filter_ele += "</select>"
                return filter_ele
    # 最后显示,date没有下拉表单;
    # 不可能将所有日期都取出来,所以自定义时间段;年内,周内...
    # 在处理这个异常中判断字段类型;
    >> b = models.CustomerInfo._meta.get_field(date)
    >> b.get_internal_type() # 获取字段类型
    DateField
    # kingadmin_tags.py
        ...
        @register.simple_tag
        def build_filter_ele(filter_column,admin_class):
            ...
            except AttributeError as e:
                print("err",e)
                if column_obj.get_internal_type in (DateField,DateTimeField):
                    today_ele = 
                
            filter_ele += "</select>"
            return filter_ele
    # 根据时间查询对象
    >> models.CustomerInfo.objects.filter(date__gte=2017-01-13)
    # kingadmin_tags.py
        @register.simple_tag
        def build_filter_ele(filter_column,admin_class):
            filter_ele = "<select name=‘%s‘>"% filter_column
            column_obj = admin_class.model._meta.get_field(filter_column)
            print("column obj:",column_obj)
            try:
                for choice in column_obj.get_choices():
                    option = "<option value=‘%s‘ %s>%s</option>" % choice
                    filter_ele += option
            except AttributeError as e:
                print("err",e)
                if column_obj.get_internal_type() in (DateField,DateTimeField):
                    time_obj = datetime.datetime.now()
                    time_list = [
                        [‘‘,------],
                        [time_obj,Today],
                        [time_obj - datetime.timedelta(7),七天内],
                        [time_obj.replace(day=1),本月],
                        [time_obj - datetime.timedelta(90),三个月内],
                        [time_obj.replace(month=1,day=1),YearToDay(YTD)],
                        [‘‘,ALL],
                    ]
                    option = ‘‘
                    for i in time_list:
                        option = "<option value=‘%s‘>%s</option>" % tuple(i)
                        filter_ele += option

            filter_ele += "</select>"
            return mark_safe(filter_ele)
    # 在页面添加input提交框;
    # 这样就显示了list_filter中的过滤条件;
##################################### 以上就生成了过滤条件

 

CRM手记-4 | Django

标签:class   ioc   gpo   imp   span   默认值   uil   tom   model   

原文地址:https://www.cnblogs.com/pymkl/p/8279724.html

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