Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1475 - filter_overrides extra now applied when field has choices #1476

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_filters/filterset.py
Expand Up @@ -404,7 +404,7 @@ def filter_for_lookup(cls, field, lookup_type):

# perform lookup specific checks
if lookup_type == 'exact' and getattr(field, 'choices', None):
return ChoiceFilter, {'choices': field.choices}
return ChoiceFilter, {'choices': field.choices, **params}

if lookup_type == 'isnull':
data = try_dbfield(DEFAULTS.get, models.BooleanField)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_filterset.py
Expand Up @@ -2,6 +2,8 @@
from unittest import mock

from django.db import models
from django.db.models import IntegerField
from django.forms.widgets import Select
from django.test import TestCase, override_settings

from django_filters.exceptions import FieldLookupError
Expand Down Expand Up @@ -242,6 +244,25 @@ class Meta:
self.assertEqual(result, BooleanFilter)
self.assertEqual(params['widget'], BooleanWidget)

def test_filter_overrides_extra_when_field_has_choices(self):
select = Select(attrs={'class': 'form-select'})

class OFilterSet(FilterSet):
class Meta:
filter_overrides = {
IntegerField: {
'filter_class': ChoiceFilter,
'extra': lambda f: {
'widget': select
},
},
}

f = User._meta.get_field('status')
result, params = OFilterSet.filter_for_lookup(f, 'exact')
self.assertEqual(result, ChoiceFilter)
self.assertEqual(params['widget'], select)


class ReverseFilterSetFilterForFieldTests(TestCase):
# Test reverse relationships for `filter_for_field`
Expand Down