Просмотр исходного кода

Fixes #23632: Treat null as IS NULL for all char filter lookups (#23686)

Arthur Hanson 6 часов назад
Родитель
Сommit
691bb37fb8
3 измененных файлов с 37 добавлено и 0 удалено
  1. 5 0
      docs/reference/filtering.md
  2. 6 0
      netbox/utilities/filters.py
  3. 26 0
      netbox/utilities/tests/test_filters.py

+ 5 - 0
docs/reference/filtering.md

@@ -107,6 +107,11 @@ Here is an example of a lookup expression on a string field that will return all
 GET /api/dcim/devices/?name__ic=switch
 ```
 
+!!! note "Filtering for null values"
+    The value `null` always matches a NULL value, regardless of the lookup expression. For
+    example, `name__ic=null` returns devices with no name, not devices whose name contains the
+    string "null".
+
 !!! note "Case-insensitive matching depends on the field's collation"
     Most `name` fields use a database collation which sorts them in natural order, so that
     `device-2` precedes `device-10`. Case-insensitive matching on those fields follows the

+ 6 - 0
netbox/utilities/filters.py

@@ -68,6 +68,12 @@ def multivalue_field_factory(field_class):
 class MultiValueCharFilter(django_filters.MultipleChoiceFilter):
     field_class = multivalue_field_factory(forms.CharField)
 
+    def get_filter_predicate(self, v):
+        # Match NULL values regardless of the lookup expression.
+        if v is None:
+            return {self.field_name: None}
+        return super().get_filter_predicate(v)
+
 
 @extend_schema_field(OpenApiTypes.DATE)
 class MultiValueDateFilter(django_filters.MultipleChoiceFilter):

+ 26 - 0
netbox/utilities/tests/test_filters.py

@@ -771,6 +771,32 @@ class DynamicFilterLookupExpressionTestCase(TestCase):
         params = {'name__nic': [' ']}
         self.assertEqual(DeviceFilterSet(params, Device.objects.all()).qs.count(), 0)
 
+    def test_device_name_null_lookups(self):
+        """
+        The null choice value must match null values for every lookup, as with exact lookups (#23632).
+        """
+        Device.objects.filter(name='Device 1').update(name=None)
+        # A name containing the literal string "null" must not match
+        Device.objects.filter(name='Device 2').update(name='Device null')
+        lookups = {
+            'name': 1,
+            'name__n': 2,
+            'name__ic': 1,
+            'name__nic': 2,
+            'name__isw': 1,
+            'name__nisw': 2,
+            'name__iew': 1,
+            'name__niew': 2,
+            'name__ie': 1,
+            'name__nie': 2,
+            'name__regex': 1,
+            'name__iregex': 1,
+        }
+        for lookup, count in lookups.items():
+            with self.subTest(lookup=lookup):
+                params = {lookup: ['null']}
+                self.assertEqual(DeviceFilterSet(params, Device.objects.all()).qs.count(), count)
+
     def test_device_mac_address_negation(self):
         params = {'mac_address__n': ['00-00-00-00-00-01', 'aa-00-00-00-00-01']}
         self.assertEqual(DeviceFilterSet(params, Device.objects.all()).qs.count(), 2)