mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-22 05:12:22 -06:00
Introduced NullableModelMultipleChoiceField to allow null filtering without causing introspection issues during database migrations
This commit is contained in:
@@ -1,15 +1,65 @@
|
||||
import django_filters
|
||||
import itertools
|
||||
|
||||
from django import forms
|
||||
from django.db.models import Q
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
|
||||
class NullableModelMultipleChoiceFilter(django_filters.MultipleChoiceFilter):
|
||||
class NullableModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
||||
"""
|
||||
This field operates like a normal ModelMultipleChoiceField except that it allows for one additional choice which is
|
||||
used to represent a value of Null. This is accomplished by creating a new iterator which first yields the null
|
||||
choice before entering the queryset iterator, and by ignoring the null choice during cleaning. The effect is similar
|
||||
to defining a MultipleChoiceField with:
|
||||
|
||||
choices = [(0, 'None')] + [(x.id, x) for x in Foo.objects.all()]
|
||||
|
||||
However, the above approach forces immediate evaluation of the queryset, which can cause issues when calculating
|
||||
database migrations.
|
||||
"""
|
||||
iterator = forms.models.ModelChoiceIterator
|
||||
|
||||
def __init__(self, null_value=0, null_label='None', *args, **kwargs):
|
||||
self.null_value = null_value
|
||||
self.null_label = null_label
|
||||
super(NullableModelMultipleChoiceField, self).__init__(*args, **kwargs)
|
||||
|
||||
def _get_choices(self):
|
||||
if hasattr(self, '_choices'):
|
||||
return self._choices
|
||||
# Prepend the null choice to the queryset iterator
|
||||
return itertools.chain(
|
||||
[(self.null_value, self.null_label)],
|
||||
self.iterator(self),
|
||||
)
|
||||
choices = property(_get_choices, forms.ChoiceField._set_choices)
|
||||
|
||||
def clean(self, value):
|
||||
# Strip all instances of the null value before cleaning
|
||||
if value is not None:
|
||||
stripped_value = [x for x in value if x != force_text(self.null_value)]
|
||||
else:
|
||||
stripped_value = value
|
||||
super(NullableModelMultipleChoiceField, self).clean(stripped_value)
|
||||
return value
|
||||
|
||||
|
||||
class NullableModelMultipleChoiceFilter(django_filters.ModelMultipleChoiceFilter):
|
||||
"""
|
||||
This class extends ModelMultipleChoiceFilter to accept an additional value which implies "is null". The default
|
||||
queryset filter argument is:
|
||||
|
||||
.filter(fieldname=value)
|
||||
|
||||
When filtering by the value representing "is null" ('0' by default) the argument is modified to:
|
||||
|
||||
.filter(fieldname__isnull=True)
|
||||
"""
|
||||
field_class = NullableModelMultipleChoiceField
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# Convert the queryset to a list of choices prefixed with a "None" option
|
||||
queryset = kwargs.pop('queryset')
|
||||
self.to_field_name = kwargs.pop('to_field_name', 'pk')
|
||||
kwargs['choices'] = [(0, 'None')] + [(getattr(o, self.to_field_name), o) for o in queryset]
|
||||
self.null_value = kwargs.get('null_value', 0)
|
||||
super(NullableModelMultipleChoiceFilter, self).__init__(*args, **kwargs)
|
||||
|
||||
def filter(self, qs, value):
|
||||
@@ -24,13 +74,13 @@ class NullableModelMultipleChoiceFilter(django_filters.MultipleChoiceFilter):
|
||||
|
||||
q = Q()
|
||||
for v in set(value):
|
||||
# Filtering on NULL
|
||||
if v == str(0):
|
||||
# Filtering by "is null"
|
||||
if v == force_text(self.null_value):
|
||||
arg = {'{}__isnull'.format(self.name): True}
|
||||
# Filtering on a related field (e.g. slug)
|
||||
elif self.to_field_name != 'pk':
|
||||
arg = {'{}__{}'.format(self.name, self.to_field_name): v}
|
||||
# Filtering on primary key
|
||||
# Filtering by a related field (e.g. slug)
|
||||
elif self.field.to_field_name is not None:
|
||||
arg = {'{}__{}'.format(self.name, self.field.to_field_name): v}
|
||||
# Filtering by primary key (default)
|
||||
else:
|
||||
arg = {self.name: v}
|
||||
if self.conjoined:
|
||||
|
||||
@@ -4,7 +4,6 @@ import re
|
||||
|
||||
from django import forms
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.db.models import Count
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
Reference in New Issue
Block a user