list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步
Posted jec1999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步相关的知识,希望对你有一定的参考价值。
1.list页面:筛选功能优化,显示choices,传condition过滤筛选项
list_filter = [
# sites.FilterOption("name",False,lambda x:x.name,lambda x:x.name),#这种配置无意义
sites.FilterOption("consultant",False,condition=Q(depart=1)),#传条件以显示筛选项
sites.FilterOption("gender",False),#choices
]
显示choices主要逻辑:如果_field.choices有值,就把_field.choices当做data_list送上去显示,而condition筛选则是不似以前无脑all()将表中对象全部显示.
def gen_list_filter(self):
for option in self.model_config.list_filter:
if option.is_func:
data_list = option.field_or_func(self.model_config,self,option)
else:
_field = self.model_config.model_class._meta.get_field(option.field_or_func)
if isinstance(_field, ForeignKey):
data_list = FilterRow(option, self, _field.rel.model.objects.filter(option.get_condition), self.model_config.request.GET)
elif isinstance(_field, ManyToManyField):
data_list = FilterRow(option, self, _field.rel.model.objects.filter(option.get_condition), self.model_config.request.GET)
elif hasattr(_field,"choices") and _field.choices:
# print(_field.choices) #((1, \'男\'), (2, \'女\'))
data_list = FilterRow(option, self, _field.choices, self.model_config.request.GET,is_choices=True)
else:
data_list = FilterRow(option, self, _field.model.objects.filter(option.get_condition), self.model_config.request.GET)
yield data_list
option.get_condition:
@property
def get_condition(self):
if self.condition:
return self.condition
else:
return Q()
2.list页面筛选与显示同步--后端获取搜索条件执行搜索返回--model_class._meta._get_fields()包含类中最全的字段.
filter_condition:获取搜索条件
@property
def filter_condition(self):
# filed1 = [i.name for i in self.model_class._meta.fields]
# print(filed1)
# print(\'------------------\')
#
# filed2 = [i.name for i in self.model_class._meta.many_to_many]
# print(filed2)
# print(\'------------------\')
filed3 = [i.name for i in self.model_class._meta._get_fields()]#包含的字段最全
# print(filed3)
# print(\'------------------\')
con = {}
for k in self.request.GET:
if k not in filed3:
continue
v = self.request.GET.getlist(k)
k = "%s__in"%k
con[k]=v
return con
以上是关于list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步的主要内容,如果未能解决你的问题,请参考以下文章
pandas使用组合条件判断数据列的内容筛选符合条件的数据行(selecting rows based on a condition in dataframe)
怎么给SharePoint得视图设置多个过滤条件? How to set multiple complex filter conditions to the SharePoint list view