5-crm项目-kingadmin,列表页---过滤

Posted andy0816

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5-crm项目-kingadmin,列表页---过滤相关的知识,希望对你有一定的参考价值。

 

展示客户列表页面--------过滤

# kingadmin/templatetags/kingadmin_tags.py
import datetime

from django.template import Library
from django.utils.safestring import mark_safe

register = Library()

@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)
    try:
        for choice in column_obj.get_choices():
            option = "<option value=‘%s‘>%s</option>"%choice
            filter_ele += option

    except AttributeError as e:
        #get_internal_type():获取字段属性
        #因为时间的过滤方式是固定的(今天,过去七天,一个月.....),而不是从后台获取的
        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‘],
            ]

            for i in time_list:
                option = "<option value=‘%s‘>%s</option>" %(i[0],i[1])
                filter_ele += option

    filter_ele += "</select>"

    return mark_safe(filter_ele)

对应的html

 <div>
            <form>
                {% for filter_column in admin_class.list_filter %}  ----->这是把自定义的所有的过滤字段进行循环读取
                    {% build_filter_ele filter_column admin_class %}
                {% endfor %}
                <input type="submit" value="过滤">
            </form>
        </div>

对应的views

def get_filter_result(request,querysets):
    filter_conditions = {}
    #获取过滤的字段
    for key,val in request.GET.items():
        if val:
            filter_conditions[key] = val
    #返回过滤后的数据
    return querysets.filter(**filter_conditions),filter_conditions


@login_required
def table_obj_list(request, app_name, model_name):
    ‘‘‘取出指定model里的数据返回给前端‘‘‘
    #拿到admin_class后,通过它找到拿到model
    admin_class = site.enable_admins[app_name][model_name]
    querysets = admin_class.model.objects.all()
    querysets,filter_conditions = get_filter_result(request,querysets)
    admin_class.filter_conditions = filter_conditions
    return render(request, ‘kingadmin/table_obj_list.html‘,{‘querysets‘:querysets,‘admin_class‘:admin_class})

 

以上是关于5-crm项目-kingadmin,列表页---过滤的主要内容,如果未能解决你的问题,请参考以下文章

kingadmin后台对象列表页功能开发

CRM项目-kingadmin批量删除

CRM项目实战-kingadmin+admin+action功能开发

2-crm项目-kingadmin

CRM项目实战-kingadmin自动发现及注册功能开发

CRM项目实战-kingadmin对象删除功能开发