Fix multiselect and multi-object CF filters

This commit is contained in:
Jeremy Stretch 2025-08-18 09:46:57 -04:00
parent a821981d27
commit 37abb4c8a8
2 changed files with 10 additions and 3 deletions

View File

@ -1691,8 +1691,8 @@ class CustomFieldModelFilterTest(TestCase):
def test_filter_multiselect(self): def test_filter_multiselect(self):
self.assertEqual(self.filterset({'cf_cf10': ['A']}, self.queryset).qs.count(), 1) self.assertEqual(self.filterset({'cf_cf10': ['A']}, self.queryset).qs.count(), 1)
self.assertEqual(self.filterset({'cf_cf10': ['A', 'C']}, self.queryset).qs.count(), 2) self.assertEqual(self.filterset({'cf_cf10': ['A', 'C']}, self.queryset).qs.count(), 2)
self.assertEqual(self.filterset({'cf_cf10': ['null']}, self.queryset).qs.count(), 1) self.assertEqual(self.filterset({'cf_cf10': ['null']}, self.queryset).qs.count(), 1) # Contains a literal null
self.assertEqual(self.filterset({'cf_cf10__empty': True}, self.queryset).qs.count(), 1) # Same as above self.assertEqual(self.filterset({'cf_cf10__empty': True}, self.queryset).qs.count(), 2)
def test_filter_object(self): def test_filter_object(self):
manufacturer_ids = Manufacturer.objects.values_list('id', flat=True) manufacturer_ids = Manufacturer.objects.values_list('id', flat=True)

View File

@ -29,6 +29,13 @@ __all__ = (
'OrganizationalModelFilterSet', 'OrganizationalModelFilterSet',
) )
STANDARD_LOOKUPS = (
'exact',
'iexact',
'in',
'contains',
)
# #
# FilterSets # FilterSets
@ -159,7 +166,7 @@ class BaseFilterSet(django_filters.FilterSet):
return {} return {}
# Skip nonstandard lookup expressions # Skip nonstandard lookup expressions
if existing_filter.method is not None or existing_filter.lookup_expr not in ['exact', 'iexact', 'in']: if existing_filter.method is not None or existing_filter.lookup_expr not in STANDARD_LOOKUPS:
return {} return {}
# Choose the lookup expression map based on the filter type # Choose the lookup expression map based on the filter type