From 52b8bd5523054a0e267a3b3c1294282409b2cf81 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 24 Sep 2024 11:28:40 -0700 Subject: [PATCH] 17558 raise validation error if removing choice from choiceset that is currently used --- netbox/extras/models/customfields.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 24d808c03..73b541a9b 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -829,10 +829,11 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel if self.extra_choices: new_choices = [obj[1] for obj in self.extra_choices] - diff_choices = list(set(original_choices) - set(new_choices)) - if diff_choices: - # CustomFields using this ChoiceSet + # only care about what fields are being deleted. + if diff_choices := list(set(original_choices) - set(new_choices)): + # Get all CustomFields using this ChoiceSet for custom_field in self.choices_for.all(): + # Then the models using those custom fields for object_type in custom_field.object_types.all(): # unfortunately querying the whole array of diff_choices doesn't work for choice in diff_choices: