From 3a21f7ab26388889875e49040099883ee5732c32 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 9 Oct 2020 11:34:50 -0400 Subject: [PATCH] Fixes #5226: Custom choice fields should be blank initially if no default choice has been designated --- docs/release-notes/version-2.9.md | 1 + netbox/extras/models/customfields.py | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/release-notes/version-2.9.md b/docs/release-notes/version-2.9.md index 538a6e2ca..6217a748b 100644 --- a/docs/release-notes/version-2.9.md +++ b/docs/release-notes/version-2.9.md @@ -17,6 +17,7 @@ * [#5220](https://github.com/netbox-community/netbox/issues/5220) - Fix API patch request against IP Address endpoint with null assigned_object_type * [#5221](https://github.com/netbox-community/netbox/issues/5221) - Fix bulk component creation for virtual machines * [#5224](https://github.com/netbox-community/netbox/issues/5224) - Don't allow a rear port to have fewer positions than the number of mapped front ports +* [#5226](https://github.com/netbox-community/netbox/issues/5226) - Custom choice fields should be blank initially if no default choice has been designated --- diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index fd09971b6..2bf4b5c86 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -200,15 +200,14 @@ class CustomField(models.Model): # Select elif self.type == CustomFieldTypeChoices.TYPE_SELECT: choices = [(cfc.pk, cfc.value) for cfc in self.choices.all()] + default_choice = self.choices.filter(value=self.default).first() - if not required: + if not required or default_choice is None: choices = add_blank_choice(choices) # Set the initial value to the PK of the default choice, if any - if set_initial: - default_choice = self.choices.filter(value=self.default).first() - if default_choice: - initial = default_choice.pk + if set_initial and default_choice: + initial = default_choice.pk field_class = CSVChoiceField if for_csv_import else forms.ChoiceField field = field_class(