CRM 日期字段过滤功能——第21天
1、CRM 日期字段过滤功能
日期这个字段不像之前的数据是有choices、ForeignKey类型,在数据库中日期是有两个类型"DateTimeField","DateField",也不像之前的有下拉框数据让你选择,
所以日期得自己先保存几个时间点,然后在判断如果是时间类型的筛选数数在select name 加上gte属于大于这个时间的数据展示在页面 <select name="_gte"></select>
先导入时间模块datetime,timedelta 使用django的时间模块如下:
from django.utils.timezone import datetime,timedelta
1.1 判断字段的类型在"DateTimeField","DateField",创建的每个时间点放在一个list里面,然后去遍历他,然后加上<option></option>,再<select name="_gte"></select>,注如果不是时间类型name是不要加上_gte的,代码如下:
if type(filed_obj).__name__ in ["DateTimeField","DateField"]: selected ='' today_ele = datetime.now().date() date_eles = [] date_eles.append(("今天",today_ele)) date_eles.append(("7天",today_ele-timedelta(days=7))) date_eles.append(("本月",today_ele.replace(day=1))) date_eles.append(("半年",today_ele-timedelta(days=180))) date_eles.append(("一年",today_ele.replace(month=1,day=1))) for dateEle in date_eles: select_ele += '''<option value='%s'%s>%s</option>''' % (dateEle[1], selected,dateEle[0]) condtion_name = "%s__gte"%(condtion) else: condtion_name = condtion
#今天 today_time = datetime.now().date() >>> today_time datetime.date(2019, 8, 11) #这个月初 >>> today_mouth = today_time.replace(day=1) >>> today_mouth datetime.date(2019, 8, 1) #今年初 >>> today_year = today_time.replace(month=1,day=1) >>> today_year datetime.date(2019, 1, 1)
完整代码如下:
@register.simple_tag def render_filter_ele(condtion,admin_class,filter_condtions): #后台返回筛选框里面的值 """ :param condtion: 需要过滤的字段名 :param admin_class: 比如Customer :param filter_condtions: 前端请求数据的json :return: """ select_ele = '''<select class="form-control" name='{condtion_name}'><option value=''>----</option>''' filed_obj = admin_class.model._meta.get_field(condtion) #判断如果是choices类型 if filed_obj.choices: selected='' for choice_item in filed_obj.choices: print("choice", choice_item, filter_condtions.get(condtion), type(filter_condtions.get(condtion))) if filter_condtions.get(condtion) == str(choice_item[0]): selected = "selected" select_ele+='''<option value='%s' %s>%s</option>'''%(choice_item[0],selected,choice_item[1]) selected='' #判断如果是属于ForeignKey类型的时候 if type(filed_obj).__name__ == 'ForeignKey': selected = '' for choice_item in filed_obj.get_choices()[1:]: if filter_condtions.get(condtion) == str(choice_item[0]): selected ="selected" select_ele += '''<option value='%s' %s>%s</option>''' % (choice_item[0], selected, choice_item[1]) selected = '' #判断是属于日期类型的时候 if type(filed_obj).__name__ in ["DateTimeField","DateField"]: selected ='' today_ele = datetime.now().date() date_eles = [] date_eles.append(("今天",today_ele)) date_eles.append(("7天",today_ele-timedelta(days=7))) date_eles.append(("本月",today_ele.replace(day=1))) date_eles.append(("半年",today_ele-timedelta(days=180))) date_eles.append(("一年",today_ele.replace(month=1,day=1))) for dateEle in date_eles: select_ele += '''<option value='%s'%s>%s</option>''' % (dateEle[1], selected,dateEle[0]) condtion_name = "%s__gte"%(condtion) else: condtion_name = condtion select_ele+="<select/>" select_ele= select_ele.format(condtion_name=condtion_name) return mark_safe(select_ele)