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

Fixes: #5315 - Make "null_option" on DynamicModelChoiceField also null the value on the model. (#5704)

Fixes: #5315 - Fix site unassignment from VLAN when using "None" option
Daniel Sheppard 5 лет назад
Родитель
Сommit
3d3748d6f5
2 измененных файлов с 11 добавлено и 1 удалено
  1. 1 0
      docs/release-notes/version-2.10.md
  2. 10 1
      netbox/utilities/forms/fields.py

+ 1 - 0
docs/release-notes/version-2.10.md

@@ -4,6 +4,7 @@
 
 ### Bug Fixes
 
+* [#5315](https://github.com/netbox-community/netbox/issues/5315) - Fix site unassignment from VLAN when using "None" option
 * [#5626](https://github.com/netbox-community/netbox/issues/5626) - Fix REST API representation for circuit terminations connected to non-interface endpoints
 * [#5716](https://github.com/netbox-community/netbox/issues/5716) - Fix filtering rack reservations by custom field
 * [#5718](https://github.com/netbox-community/netbox/issues/5718) - Fix bulk editing of services when no port(s) are defined

+ 10 - 1
netbox/utilities/forms/fields.py

@@ -5,6 +5,7 @@ from io import StringIO
 
 import django_filters
 from django import forms
+from django.conf import settings
 from django.forms.fields import JSONField as _JSONField, InvalidJSONInput
 from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
 from django.db.models import Count
@@ -355,7 +356,15 @@ class DynamicModelChoiceField(DynamicModelChoiceMixin, forms.ModelChoiceField):
     Override get_bound_field() to avoid pre-populating field choices with a SQL query. The field will be
     rendered only with choices set via bound data. Choices are populated on-demand via the APISelect widget.
     """
-    pass
+
+    def clean(self, value):
+        """
+        When null option is enabled and "None" is sent as part of a form to be submitted, it is sent as the
+        string 'null'.  This will check for that condition and gracefully handle the conversion to a NoneType.
+        """
+        if self.null_option is not None and value == settings.FILTERS_NULL_CHOICE_VALUE:
+            return None
+        return super().clean(value)
 
 
 class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultipleChoiceField):