Reputation: 15084
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. 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. Modifying the __init__
of the form:
def __init__(self, *args, **kwargs): super(ApplicationFilter, self).__init__(*args, **kwargs) self.form.initial['status']='NEW' self.form.fields['status'].initial='NEW'-- again the form rendered with the default initial value (All)... Also tried setting the value as
['NEW']
-- again no luck.
Does anybody know how should this be handled ? I am using the latest (from github) version of django-filter.
TIA
Upvotes: 3
Views: 2785
Reputation: 874
This answer might work for you: Set initial value with django-filters?
In my views, I do:
get_query = request.GET.copy()
if 'status' not in get_query:
get_query['status'] = 'final'
filter_set = MatterFilterSet(get_query)
Upvotes: 0
Reputation: 11
Try this:
def __init__(self, *args, **kwargs):
super(ApplicationFilter, self).init(*args, **kwargs)
self.initial['status'] = 'NEW'
Upvotes: 0