django admin custom list_filter [英] django admin custom list_filter
本文介绍了django admin custom list_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我升级到1.3版本,此代码不再工作:
class CustomChoiceFilterSpec(ChoicesFilterSpec):
/ pre>
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#更改此值以匹配对象的搜索
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 model.objects.values中的项目(f.name).annotate(count = Count(' pk'))
self.foreign_key_count [item [f.name]] = item ['count']
def choice(self,cl):
yield {'选择':self.lookup_val为None,
'query_string':cl.get_query_string({},[self.lookup_kwarg]),
'显示':('All')}
items = set([getattr(i,self.foreign_key)for i in self.objects])
在项目中的k:
如果k是没有:
kk =无
else:
kk = k.id
yield {'selected':smart_unicode(kk)== self.lookup_val,
'query_string ':cl.get_query_string({self.lookup_kwarg:kk}),#更改.id以匹配您要搜索的内容
'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'
请帮助我
Fabio
解决方案
http://djangosnippets.org/snippets/2194/
I did the upgrade to 1.3 version and this code doesn't work any longer:
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))
The error I get is: init() got an unexpected keyword argument 'field_path'
Please help me,
Fabio
解决方案Found the fix here:-
http://djangosnippets.org/snippets/2194/
这篇关于django admin custom list_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文