0
我沒有升級到1.3版本,此代碼不能再工作下去:Django管理定製list_filter
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」
請幫助我,
法比奧
優秀。什麼是修復? – peter2108
查看第一條評論。您需要修復'__init __()'參數,因爲在1.3中添加了field_path。 – k4ml