From 3a7a4f83d6201772db23a0c4a93582c54256cd00 Mon Sep 17 00:00:00 2001 From: Brian Tiemann Date: Tue, 12 Nov 2024 20:44:35 -0500 Subject: [PATCH] Enforce saving only a single is_primary MACAddress per interface/vminterface --- netbox/dcim/forms/model_forms.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/netbox/dcim/forms/model_forms.py b/netbox/dcim/forms/model_forms.py index d036507ba..23a12ff0e 100644 --- a/netbox/dcim/forms/model_forms.py +++ b/netbox/dcim/forms/model_forms.py @@ -923,6 +923,14 @@ class MACAddressForm(NetBoxModelForm): super().__init__(*args, **kwargs) + def clean_is_primary(self): + # If setting this MACAddress to primary, unset all the rest on this interface + if is_primary := self.cleaned_data['is_primary']: + if interface := self.cleaned_data['interface']: + interface.mac_addresses.all().update(is_primary=False) + if vminterface := self.cleaned_data['vminterface']: + vminterface.mac_addresses.all().update(is_primary=False) + return is_primary def clean(self): super().clean()