标签:循环 客户 tag mon oda erro crm cts djang
# 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})
标签:循环 客户 tag mon oda erro crm cts djang
原文地址:https://www.cnblogs.com/andy0816/p/13471422.html