文章目录
- django常用的组合搜索组件
- 快速使用
- 配置信息
- 1. 视图函数
- 2. 前端模板
- 3. css样式
- 代码实现
django常用的组合搜索组件
在项目开发中,如果有大量数据就要用到组合搜索,通过组合搜索对大块内容进行分类筛选。
快速使用
三步走:(其实主要就是传入配置信息)
- 创建组合搜索类的实例对象,传入参数进行配置(三个参数:
request
,model_class
,*options
) - 在数据库查询时传入search_group.get_condition方法生成的查询条件(该方法会根据传入的配置生成查询条件)
- 在前端使用search_group.get_row_list(search_group.get_row_list生成一个列表,包含所有传入的Option字段的信息)
配置信息
这是Option类的init函数:
class Option(object):def __init__(self, field, is_condition=True, is_multi=False, db_condition=None, text_func=None, value_func=None):""":param field: 组合搜索关联的字段(支持choice/Foreignkey类型):param is_multi: 是否支持多选:param db_condition: 数据库关联查询时的条件(字典):param text_func: 此函数用于显示组合搜索按钮页面文本(页面展示的值):param value_func: 此函数用于显示组合搜索按钮值(url中的参数值)"""self.field = fieldself.is_condition = is_conditionself.is_multi = is_multiif not db_condition:db_condition = {}self.db_condition = db_conditionself.text_func = text_funcself.value_func = value_funcself.is_choice = False
示例:
search_group = NbSearchGroup(request,models.TransactionRecord,Option('charge_type', is_multi=True, text_func=lambda x: x[1],value_func=lambda x:x[0]), # choiceOption('customer', db_condition={"id__lte": 10, 'active': 1}, text_func=lambda x: x.username,value_func=lambda x:x.id), # fk)
1. 视图函数
from django.shortcuts import render
from web import models
from utils.pager import Pagination
from django.db.models import Q
from utils.group import Option, NbSearchGroupdef my_transaction_list(request):# 第一步:配置和传参search_group = NbSearchGroup(request,models.TransactionRecord,Option('charge_type'), # choice)# ...其他的查询条件# 第二步:获取条件 .filter(**search_group.get_condition)queryset = models.TransactionRecord.objects.filter(**search_group.get_condition).filter(其他查询条件)pager = Pagination(request, queryset) # 分页组件context = {"pager": pager,"keyword": keyword,"search_group": search_group # 第三步:将对象传入前端页面}return render(request, 'my_transaction_list.html', context)
2. 前端模板
{% if search_group.get_row_list %}<div class="panel panel-default"><div class="panel-heading"><i class="fa fa-filter" aria-hidden="true"></i> 快速筛选</div><div class="panel-body"><div class="search-group">{% for row in search_group.get_row_list %}<div class="row">{% for obj in row %}{{ obj|safe }}{% endfor %}</div>{% endfor %}</div></div></div>
{% endif %}
3. css样式
.search-group {padding: 5px 10px;
}.search-group .row .whole {width: 60px;float: left;display: inline-block;padding: 5px 0 5px 8px;margin: 3px;font-weight: bold;text-align: right;}.search-group .row .others {padding-left: 80px;
}.search-group .row a {display: inline-block;padding: 5px 8px;margin: 3px;border: 1px solid #d4d4d4;}.search-group .row a {display: inline-block;padding: 5px 8px;margin: 3px;border: 1px solid #d4d4d4;
}.search-group a.active {color: #fff;background-color: #337ab7;border-color: #2e6da4;
}
代码实现
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.db.models import ForeignKey, ManyToManyFieldclass SearchGroupRow(object):def __init__(self, title, queryset_or_tuple, option, query_dict):""":param title: 组合搜索的列名称:param queryset_or_tuple: 组合搜索关联获取到的数据:param option: 配置:param query_dict: request.GET"""self.title = titleself.queryset_or_tuple = queryset_or_tupleself.option = optionself.query_dict = query_dictdef __iter__(self):yield '<div class="whole">'yield self.titleyield '</div>'yield '<div class="others">'total_query_dict = self.query_dict.copy()total_query_dict._mutable = Trueorigin_value_list = self.query_dict.getlist(self.option.field)if not origin_value_list:yield "<a class='active' href='?%s'>全部</a>" % total_query_dict.urlencode()else:total_query_dict.pop(self.option.field)yield "<a href='?%s'>全部</a>" % total_query_dict.urlencode()for item in self.queryset_or_tuple:text = self.option.get_text(item)value = str(self.option.get_value(item))query_dict = self.query_dict.copy()query_dict._mutable = Trueif not self.option.is_multi:query_dict[self.option.field] = valueif value in origin_value_list:query_dict.pop(self.option.field)yield "<a class='active' href='?%s'>%s</a>" % (query_dict.urlencode(), text)else:yield "<a href='?%s'>%s</a>" % (query_dict.urlencode(), text)else:# {'gender':['1','2']}multi_value_list = query_dict.getlist(self.option.field)if value in multi_value_list:multi_value_list.remove(value)query_dict.setlist(self.option.field, multi_value_list)yield "<a class='active' href='?%s'>%s</a>" % (query_dict.urlencode(), text)else:multi_value_list.append(value)query_dict.setlist(self.option.field, multi_value_list)yield "<a href='?%s'>%s</a>" % (query_dict.urlencode(), text)yield '</div>'class Option(object):def __init__(self, field, is_condition=True, is_multi=False, db_condition=None, text_func=None, value_func=None):""":param field: 组合搜索关联的字段:param is_multi: 是否支持多选:param db_condition: 数据库关联查询时的条件:param text_func: 此函数用于显示组合搜索按钮页面文本:param value_func: 此函数用于显示组合搜索按钮值"""self.field = fieldself.is_condition = is_conditionself.is_multi = is_multiif not db_condition:db_condition = {}self.db_condition = db_conditionself.text_func = text_funcself.value_func = value_funcself.is_choice = Falsedef get_db_condition(self, request, *args, **kwargs):return self.db_conditiondef get_queryset_or_tuple(self, model_class, request, *args, **kwargs):"""根据字段去获取数据库关联的数据:return:"""# 根据gender或depart字符串,去自己对应的Model类中找到 字段对象field_object = model_class._meta.get_field(self.field)title = field_object.verbose_name# 获取关联数据if isinstance(field_object, ForeignKey) or isinstance(field_object, ManyToManyField):# FK和M2M,应该去获取其关联表中的数据: QuerySetdb_condition = self.get_db_condition(request, *args, **kwargs)return SearchGroupRow(title,field_object.remote_field.model.objects.filter(**db_condition),self,request.GET)else:# 获取choice中的数据:元组self.is_choice = Truereturn SearchGroupRow(title, field_object.choices, self, request.GET)def get_text(self, field_object):"""获取文本函数:param field_object::return:"""if self.text_func:return self.text_func(field_object)if self.is_choice:return field_object[1]return str(field_object)def get_value(self, field_object):if self.value_func:return self.value_func(field_object)if self.is_choice:return field_object[0]return field_object.pkdef get_search_condition(self, request):if not self.is_condition:return Noneif self.is_multi:values_list = request.GET.getlist(self.field) # tags=[1,2]if not values_list:return Nonereturn '%s__in' % self.field, values_listelse:value = request.GET.get(self.field) # tags=[1,2]if not value:return Nonereturn self.field, valueclass NbSearchGroup(object):""" 最后的封装应用类,使用时三步走 """def __init__(self, request, model_class, *options):self.request = requestself.model_class = model_classself.options = optionsdef get_row_list(self):row_list = []for option_object in self.options:row = option_object.get_queryset_or_tuple(self.model_class, self.request)row_list.append(row)return row_list@propertydef get_condition(self):""":return: 获取组合搜索的条件"""condition = {}# ?depart=1&gender=2&page=123&q=999for option in self.options:key_and_value = option.get_search_condition(self.request)if not key_and_value:continuekey, value = key_and_valuecondition[key] = valuereturn condition
若有错误与不足请指出,关注DPT一起进步吧!!!