CRM多条件筛选和分页——第16天
---恢复内容开始---
1.1 CRM先显示要过滤的字段
前端代码如下:
{% for condtion in admin_class.list_filters%}
<div class="col-lg-2">
<span>{{condtion}}</span>
</div>
在kind_admin.py文件中在Customer(admin_class)类中已经定义list_filters筛选的字段值,在.html文件中循环遍历展示过滤的字段
<div class="row">
<form class="" method="get">
{% for condtion in admin_class.list_filters%}
<div class="col-lg-2">
<span>{{condtion}}</span>
{% render_filter_ele condtion admin_class filter_condtions %}
</div>
{% endfor %}
<button type="SUBMIT" class="btn btn-success">检索</button>
</form>
</div>
1.2 CRM 显示过滤字段的值,利用form表单和get的方式请求
方式:1、利用form表单和get方式请求
2、在app名kind_admin文件下新建一个utils.py文件用于保存前端请求的数据放在一个变量名为dictfilter_condtions的dict类型
3、在view.py文件中返回admin_class,filter_condtions
4、在自定义标签tags.py里面判断过滤的值展示,在前端传入tags.py 里面函数的调用
1、利用form表单和get方式请求,前端代码table_objs.html文件:
<div class="row">
<form class="" method="get">
{% for condtion in admin_class.list_filters%}
<div class="col-lg-2">
<span>{{condtion}}</span>
{% render_filter_ele condtion admin_class filter_condtions %}
</div>
{% endfor %}
<button type="SUBMIT" class="btn btn-success">检索</button>
</form>
</div>
2、在app名kind_admin文件下新建一个utils.py文件用于保存前端请求的数据放在一个dictfilter_condtions里面,uttils.py文件代码
这个是请求数据的过滤
admin_class.model.objects.filter(**filter_conditions)相当于
>>> m={"source":0}
>>> models.Customer.objects.filter(**m)
<QuerySet [<Customer: 5676567@qq.com>]> 或者
如果dict里面没有数据,会筛选所有的数据如下:
>>> m={}
>>> models.Customer.objects.filter(**m)
<QuerySet [<Customer: 5676567@qq.com>, <Customer: 25064568@qq.com>, <Customer: 787874859@qq.com>, <Customer: 2165
64732@qqcom>]>
def table_filter(request,admin_class):
"""进行条件过滤并返回过滤后的数据"""
filter_conditions = {}
for k,v in request.GET.items():
if v:
filter_conditions[k]=v
print("filter_conditions:",filter_conditions)
print("admin_class.model.objects.filter(**filter_conditions)",admin_class.model.objects.filter(**filter_conditions))
return admin_class.model.objects.filter(**filter_conditions),filter_conditions
3、在view.py文件中返回admin_class,filter_condtions,view.py代码如下:
def display_table_objs(request,app_name,table_name):
print("-->",app_name,table_name)
#models_module = importlib.import_module('%s.models'%(app_name))
#model_obj = getattr(models_module,table_name)
admin_class = kind_admin.enabled_admins[app_name][table_name]
#admin_class = king_admin.enabled_admins[crm][userprofile]
#object_list = admin_class.model.objects.all()
object_list,filter_condtions = table_filter(request,admin_class)
paginator = Paginator(object_list, admin_class.list_per_page) # Show 25 contacts per page
print("paginator------",paginator)
page = request.GET.get('page')
try:
query_sets = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
query_sets = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
query_sets = paginator.page(paginator.num_pages)
print("query_sets------------",query_sets)
return render(request,"kindadmin/table_objs.html",{"admin_class":admin_class,
"query_sets":query_sets,
"filter_condtions":filter_condtions})
有部分page代码是分页的代码
4、在自定义标签tags.py里面判断过滤的值展示,在前端传入tags.py 里面函数的调用,tags.py文件代码:
4.1 有判断这个这类Customer里面字段的属性,如是有choices属性,或者是ForeignKey属性,
4.2 其他属性的没有判断,导致其他有值的字段没有显示数据筛选
如果请求数据的值等于循环筛选框的值那么这个就是被选中有selected属性,没有这无这个属性
查找类里面字段的属性信息
filed_obj = admin_class.model._meta.get_field(condtion) 相当于
>>> m = models.Customer._meta.get_field("source")
>>> m
<filed_obj.choices.db.models.fields.SmallIntegerField: source>
类属性信息里面是否有choices :filed_obj.choices 相当于:
>>> m.choices
((0, '转介绍'), (1, 'QQ群'), (2, '官网'), (3, '百度推广'),
type(filed_obj).__name__查找属性的类型 相当于:
>>> type(m).__name__
'ForeignKey'
如果是外键的时候的用get_choices取值如
filed_obj.get_choices()[1:]: 相当于
>>> m.get_choices()
[('', '---------'), (1, 'python')]
前面list[0]是上面的标题,所以的从list[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='%s'><option value=''>----</option>'''%(condtion)
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 = ''
select_ele+="<select/>"
return mark_safe(select_ele)
剩下的分页明天在写吧,昨天睡觉都在想着那个分页为什么那里错了