django admin custom list_filter

て烟熏妆下的殇ゞ 提交于 2019-12-23 04:29:25

问题


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


回答1:


Found the fix here:-

http://djangosnippets.org/snippets/2194/



来源:https://stackoverflow.com/questions/6740398/django-admin-custom-list-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!