CRM手记-4 | Django

Posted 胡说八道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CRM手记-4 | Django相关的知识,希望对你有一定的参考价值。

# 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的主要内容,如果未能解决你的问题,请参考以下文章

CRM手记-9 | Django

CRM手记-1 | Django

CRM手记-2 | Django

CRM手记-5 | Django

CRM手记- 11 | Django

CRM手记-8 | Django