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

Fixes #4240: Fix exception when filtering foreign keys by NULL

This commit is contained in:
Jeremy Stretch
2020-02-21 21:38:25 -05:00
parent 1c72d75b62
commit 04ee55a40c
2 changed files with 9 additions and 8 deletions

View File

@ -11,6 +11,7 @@
* [#4232](https://github.com/netbox-community/netbox/issues/4232) - Enforce consistent background striping in rack elevations
* [#4235](https://github.com/netbox-community/netbox/issues/4235) - Fix API representation of `content_type` for export templates
* [#4239](https://github.com/netbox-community/netbox/issues/4239) - Fix exception when selecting all filtered objects during bulk edit
* [#4240](https://github.com/netbox-community/netbox/issues/4240) - Fix exception when filtering foreign keys by NULL
---

View File

@ -2,8 +2,9 @@ import csv
import json
import re
from io import StringIO
import yaml
import django_filters
import yaml
from django import forms
from django.conf import settings
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
@ -564,18 +565,17 @@ class TagFilterField(forms.MultipleChoiceField):
class DynamicModelChoiceMixin:
field_modifier = ''
filter = django_filters.ModelChoiceFilter
def get_bound_field(self, form, field_name):
bound_field = BoundField(form, self, field_name)
# Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
# will be populated on-demand via the APISelect widget.
field_name = '{}{}'.format(self.to_field_name or 'pk', self.field_modifier)
if bound_field.data:
self.queryset = self.queryset.filter(**{field_name: self.prepare_value(bound_field.data)})
elif bound_field.initial:
self.queryset = self.queryset.filter(**{field_name: self.prepare_value(bound_field.initial)})
data = bound_field.data or bound_field.initial
if data:
filter = self.filter(field_name=self.to_field_name or 'pk', queryset=self.queryset)
self.queryset = filter.filter(self.queryset, data)
else:
self.queryset = self.queryset.none()
@ -594,7 +594,7 @@ class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultip
"""
A multiple-choice version of DynamicModelChoiceField.
"""
field_modifier = '__in'
filter = django_filters.ModelMultipleChoiceFilter
class LaxURLField(forms.URLField):