初始值不适用于django-filters上的ChoiceField [英] Initial value is not working for ChoiceField on django-filters

查看:64
本文介绍了初始值不适用于django-filters上的ChoiceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下django-filter( https://github.com/alex/django-过滤器/)过滤器:

I've the following django-filter (https://github.com/alex/django-filter/) filter:


class ApplicationFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=STATUS2,)

状态包含以下元组列表:

with status containing the following tuple list:


STATUS_CHOICES = (
    ( '', u'All'),
    ( 'NEW', u'New'),
    ( 'SUBMIT', u'Submit'),
    ( 'CANCEL', u'Cancel'),
)

现在,我想为该过滤器设置一个不同于空过滤器(所有)的初始值.因此,我尝试了以下操作,但都没有成功:

Now, I'd like to set an initial value for that filter different than the empty one (All). So I tried the following things, all without success:

i.向该字段添加初始参数: status = django_filters.ChoiceFilter(choices = STATUS2,initial ='NEW')或带有数组 status = django_filters.ChoiceFilter(choices = STATUS2,initial = ['NEW']).使用默认初始值呈现的表单.

i. Adding an initial parameter to the field: status = django_filters.ChoiceFilter(choices=STATUS2, initial = 'NEW' ) or with an array status = django_filters.ChoiceFilter(choices=STATUS2, initial = ['NEW'] ). The form rendered with the default initial value.

ii.修改表单的 __ init __ :


    def __init__(self, *args, **kwargs):
        super(ApplicationFilter, self).__init__(*args, **kwargs)
        self.form.initial['status']='NEW'
        self.form.fields['status'].initial='NEW'

-再次使用默认初始值(全部)呈现的表单...还尝试将值设置为 ['NEW'] -再次没有运气.

-- again the form rendered with the default initial value (All)... Also tried setting the value as ['NEW'] -- again no luck.

有人知道该如何处理吗?我正在使用django-filter的最新版本(来自github).

Does anybody know how should this be handled ? I am using the latest (from github) version of django-filter.

TIA

推荐答案

此答案可能对您有用:是否使用django-filters设置初始值?

This answer might work for you: Set initial value with django-filters?

我认为:

get_query = request.GET.copy()
if 'status' not in get_query:
    get_query['status'] = 'final'
filter_set = MatterFilterSet(get_query)

这篇关于初始值不适用于django-filters上的ChoiceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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