django 管理员自定义 list_filter
Posted
技术标签:
【中文标题】django 管理员自定义 list_filter【英文标题】:django admin custom list_filter 【发布时间】:2011-10-08 02:20:38 【问题描述】:我升级到 1.3 版本,这段代码不再工作了:
class CustomChoiceFilterSpec(ChoicesFilterSpec):
def __init__(self, f, request, params, model, model_admin):
super(CustomChoiceFilterSpec, self).__init__(f, request, params, model, model_admin)
self.lookup_kwarg = '%s__id__exact' % f.name # Change this to match the search of your object
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
self.objects = model.objects.all()
self.foreign_key = f.name
self.foreign_key_count =
for item in model.objects.values(f.name).annotate(count=Count('pk')):
self.foreign_key_count[item[f.name]] = item['count']
def choices(self, cl):
yield 'selected': self.lookup_val is None,
'query_string': cl.get_query_string(, [self.lookup_kwarg]),
'display': ('All')
items = set([getattr(i, self.foreign_key) for i in self.objects])
for k in items:
if k is None:
kk = None
else:
kk = k.id
yield 'selected': smart_unicode(kk) == self.lookup_val,
'query_string': cl.get_query_string(self.lookup_kwarg: kk), # Change .id to match what you are searching for
'display': '%s (%s)' % (k, self.foreign_key_count[kk])
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'compact_filter', False), CustomChoiceFilterSpec))
我得到的错误是:init() 得到了一个意外的关键字参数 'field_path'
请帮帮我,
法比奥
【问题讨论】:
【参考方案1】:在这里找到了解决方法:-
http://djangosnippets.org/snippets/2194/
【讨论】:
优秀。解决方法是什么? 查看第一条评论。您需要修复__init__()
参数,因为在 1.3 中添加了 field_path
。以上是关于django 管理员自定义 list_filter的主要内容,如果未能解决你的问题,请参考以下文章