Skip to content

Commit

Permalink
fix carltongibson#1475 - filter_overrides extra now applied when fiel…
Browse files Browse the repository at this point in the history
…d has choices
  • Loading branch information
chris.brett committed Feb 9, 2022
1 parent 3635b2b commit ce47077
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_filters/filterset.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit ce47077

Please sign in to comment.