djangofilter查询多选_djangoadmin列表页⾯,右侧筛选框实现
多选功能
内容如题, 但是切记admin页⾯请勿定制得过于复杂. 复杂页⾯请⾃⾏设计
理念: 将url中的'='替换为'__in='
1 在根⽬录下, 新建⼀个⽂件夹 extraadminfilters, 在这个⽂件夹下新增两个⽂件__init__.py, 声明这个⽂件夹是⼀个包; 'filters.py', 写我们的额外的Filter
filters.py代码如下
from anslation import ugettext_lazy as _
ib.admin.filters import FieldListFilter
from dels.fields import IntegerField, AutoField
class MultipleSelectFieldListFilter(FieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
self.lookup_kwarg = '%s_filter' % field_path
self.filter_statement = '%s__id' % field_path
self.lookup_val = (self.lookup_kwarg, None)
self.lookup_choices = _choices(include_blank=False)
super(MultipleSelectFieldListFilter, self).__init__(
field, request, params, model, model_admin, field_path)
def expected_parameters(self):
return [self.lookup_kwarg]
def values(self):
"""
Returns a list of values to filter on.
"""
values = []
value = self.(self.lookup_kwarg, None)
if value:
values = value.split(',')
# convert to integers if IntegerField
if type(del._meta.pk) in [IntegerField, AutoField]:
values = [int(x) for x in values]
return values
def queryset(self, request, queryset):
raise NotImplementedError
def choices(self, cl):
# ib.admin.views.main import EMPTY_CHANGELIST_VALUE yield {
'selected': self.lookup_val is None,
'query_string': cl.get_query_string({},
[self.lookup_kwarg]),
'display': _('All')
}
for pk_val, val in self.lookup_choices:
selected = pk_val in self.values()
pk_list = set(self.values())
if selected:
ve(pk_val)
else:
pk_list.add(pk_val)
queryset_value = ','.join([str(x) for x in pk_list])
if pk_list:
query_string = cl.get_query_string({
self.lookup_kwarg: queryset_value,
})
else:
query_string = cl.get_query_string({}, [self.lookup_kwarg])
yield {
'selected': selected,
'query_string': query_string,
'display': val,
}
class IntersectionFieldListFilter(MultipleSelectFieldListFilter):
"""
A FieldListFilter which allows multiple selection of
filters for many-to-many type fields. A list of objects will be
returned whose m2m contains all the selected filters.
"""
def queryset(self, request, queryset):
for value in self.values():
filter_dct = {
self.filter_statement: value
}
queryset = queryset.filter(**filter_dct)
return queryset
class UnionFieldListFilter(MultipleSelectFieldListFilter):
"""
A FieldListFilter which allows multiple selection of
filters for many-to-many type fields. A list of objects will be
returned whose m2m contains all the selected filters.
django admin 自定义页面"""
def queryset(self, request, queryset):
filter_statement = "%s__in" % self.filter_statement
filter_statement = place('__id', '')
filter_values = self.values()
filter_dct = {
filter_statement: filter_values
}
if filter_values:
return queryset.filter(**filter_dct)
else:
return queryset
2 在需要使⽤多选的app对应的admin.py⽂件中, 引⼊刚刚新增的Filter
from extraadminfilters.filters import UnionFieldListFilter
3 在需要进⾏多重筛选的model对应admin的list_filter中进⾏修改成如下样式
list_filter的元素可以是单个的字段, 也能是⼀个包含两个元素的元组, 其中元组的的⼀个元素是字段名称, 第⼆是应⽤的Filter类list_filter = (
'creater',
'status',
('shipping', UnionFieldListFilter),
'group',
)
此时, admin页⾯中已经可以进⾏多个筛选了, 再选择⼀次即可取消
Paste_Image.png
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论