django choicefield过滤器在管理面板 [英] django choicefield filter in admin panel

查看:130
本文介绍了django choicefield过滤器在管理面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,django管理员的 list_filter 提供所有型号选择中可用的过滤器。但是除了那些我想要的过滤器之外,让我们说它是无过滤器。

by default django admin's list_filter provide all filters available in model choices. but apart from those I want one more filter, lets say it 'None' filter.

class Mymodel:
    char choice field (choices=(('1', 'txt1', '2', 'txt2')), null=True)

class MymodelAdmin(admin.ModelAdmin):
    ...
    list_filter = [..., choice_field, ...]
    ...

这将在管理面板中设置三个过滤器(右侧过滤器), All,'txt1','txt2'。对?
如果没有从选项中分配任何值,我还想要一个过滤器无。

this will set three filter in admin panel(right hand side filters) , All, 'txt1', 'txt2'. right? I want one more filter 'None' if no value is assign from choices.

到目前为止我尝试过的操作。.

what I tried so far..

class ChoiceFieldFilter(admin.filters.ChoicesFieldListFilter):

    def __init__(self, *args, **kwargs):
        super(ChoiceFieldFilter, self).__init__(*args, **kwargs)

        self.lookup_val = [('', 'None')]

    def queryset(self, request, queryset):
        print self.lookup_val
        print  self.field.flatchoices
        if self.lookup_val == '':
            return queryset.filter(choice_field='')
        else:
            return queryset.filter(choice_field=self.lookup_val)

    def choices(self, cl):
        pass

然后在管理类中

list_filter = [..., ('choice_field', ChoiceFieldFilter), ...]

但它不起作用,我无法在djan中看到 None 过滤器go admin

but its not working, I'm unable to see None filter in django admin

推荐答案

默认情况下,admin.AllValuesFieldListFilter返回选择的值,而不是选择的详细名称。因此,要解决此问题,请使用修改后的admin.AllValuesFieldListFilter。

By default the admin.AllValuesFieldListFilter return a value of a choice, not verbose name of the choice. So, for resolve it use the modified admin.AllValuesFieldListFilter.

class AllValuesChoicesFieldListFilter(admin.AllValuesFieldListFilter):

    def choices(self, changelist):
        yield {
            'selected': self.lookup_val is None and self.lookup_val_isnull is None,
            'query_string': changelist.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        include_none = False

        # all choices for this field
        choices = dict(self.field.choices)

        for val in self.lookup_choices:
            if val is None:
                include_none = True
                continue
            val = smart_text(val)
            yield {
                'selected': self.lookup_val == val,
                'query_string': changelist.get_query_string({
                    self.lookup_kwarg: val,
                }, [self.lookup_kwarg_isnull]),

                # instead code, display title
                'display': choices[val],
            }
        if include_none:
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': changelist.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': self.empty_value_display,
            }

用法:

list_filter = (
        ('country_origin', AllValuesChoicesFieldListFilter),
    )

这篇关于django choicefield过滤器在管理面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆