From 9f4b9b34765f05a23056fc6bedc85c358e3a2a35 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Tue, 7 Sep 2021 18:28:49 -0700 Subject: [PATCH] #7205: Handle `null_option` in dynamic multi-select choices field --- netbox/utilities/forms/fields.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/netbox/utilities/forms/fields.py b/netbox/utilities/forms/fields.py index 05eaa9515..6684eb908 100644 --- a/netbox/utilities/forms/fields.py +++ b/netbox/utilities/forms/fields.py @@ -477,3 +477,13 @@ class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultip """ filter = django_filters.ModelMultipleChoiceFilter widget = widgets.APISelectMultiple + + def clean(self, value: list[str]): + """ + 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 settings.FILTERS_NULL_CHOICE_VALUE in value: + value = [v for v in value if v != settings.FILTERS_NULL_CHOICE_VALUE] + return [self.null_option, *value] + return super().clean(value)