You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ModelChoiceFilter and ModelMultipleChoiceFilter don't accept empty_label argument like ChoiceFilter and MultipleChoiceFilter do.
I work around this problem by overriding form filed empty label in __init__ method like self.form.fields['filed_name'].empty_label = 'Custom label'. I can live with it but there is very strange issue - if I override the filter querysets as well like:
class MyModelFilter(FilterSet):
user = django_filters.ModelMultipleChoiceFilter(queryset=None)
project = django_filters.ModelMultipleChoiceFilter(queryset=None)
class Meta:
model = MyModel
fields = ['user', 'project']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.filters['user'].queryset = User.objects.custom()
self.form.fields['user'].empty_label = 'Custom label'
self.filters['project'].queryset = Project.objects.custom()
self.form.fields['project'].empty_label = 'Custom label'
I get an error: 'NoneType' object has no attribute 'none' but if I put all the empty labels overrides AFTER all the queryset overrides like:
while I was digging into your issue I could reproduce it for the ModelMultipleChoiceFilter but not for the ModelChoiceFilter, with my implementation you can use the following approach without raising any error.
Now I guess it's time to ask my question, why do you need to override the empty_label field of a ModelMultipleChoiceFilter, I guess that this should be None, right? I really want to check your case to understand your problem.
Hi, @panosangelopoulos. Sorry for late answer. My use case is to show custom label for resetting the filter. Imagine you have a filter for a field named status and you want to show something like Any status instead of dashes. Regarding to ModelChoiceFilter, it seems I was wrong.
ModelChoiceFilter and ModelMultipleChoiceFilter don't accept empty_label argument like ChoiceFilter and MultipleChoiceFilter do.
I work around this problem by overriding form filed empty label in
__init__
method likeself.form.fields['filed_name'].empty_label = 'Custom label'
. I can live with it but there is very strange issue - if I override the filter querysets as well like:I get an error:
'NoneType' object has no attribute 'none'
but if I put all the empty labels overrides AFTER all the queryset overrides like:There is no error.
The text was updated successfully, but these errors were encountered: