list頁面-按照choice篩選丶傳condition過濾篩選項丶篩選與顯示同步
阿新 • • 發佈:2017-10-28
gin posit objects ffffff part font attr cond 技術 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 conlist頁面-按照choice篩選丶傳condition過濾篩選項丶篩選與顯示同步