1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #2895: Fix filtering of nullable character fields

This commit is contained in:
Jeremy Stretch
2019-02-14 09:43:56 -05:00
parent fdbef8ee71
commit 7a0ab3aa15
2 changed files with 4 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import django_filters
from django.conf import settings
from django.db.models import Q
from taggit.models import Tag
@ -14,12 +15,11 @@ class NullableCharFieldFilter(django_filters.CharFilter):
"""
Allow matching on null field values by passing a special string used to signify NULL.
"""
null_value = 'NULL'
def filter(self, qs, value):
if value != self.null_value:
if value != settings.FILTERS_NULL_CHOICE_VALUE:
return super().filter(qs, value)
qs = self.get_method(qs)(**{'{}__isnull'.format(self.name): True})
qs = self.get_method(qs)(**{'{}__isnull'.format(self.field_name): True})
return qs.distinct() if self.distinct else qs