From 78c56c2cb83941ffff01a4b99ce4fa951ba1156b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 19 Jan 2026 11:57:35 -0500 Subject: [PATCH] Update only the primary/OOB IP fields when saving the parent object --- netbox/ipam/forms/model_forms.py | 34 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index c1ad60ff5..495c54643 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -424,34 +424,40 @@ class IPAddressForm(TenancyForm, PrimaryModelForm): def save(self, *args, **kwargs): ipaddress = super().save(*args, **kwargs) - # Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine. interface = self.instance.assigned_object if type(interface) in (Interface, VMInterface): parent = interface.parent_object parent.snapshot() + update_fields = [] + + # Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine. if self.cleaned_data['primary_for_parent']: if ipaddress.address.version == 4: parent.primary_ip4 = ipaddress + update_fields.append('primary_ip4') else: parent.primary_ip6 = ipaddress - parent.save() + update_fields.append('primary_ip6') elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress: parent.primary_ip4 = None - parent.save() + update_fields.append('primary_ip4') elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress: parent.primary_ip6 = None - parent.save() + update_fields.append('primary_ip6') - # Assign/clear this IPAddress as the OOB for the associated Device - if type(interface) is Interface: - parent = interface.parent_object - parent.snapshot() - if self.cleaned_data['oob_for_parent']: - parent.oob_ip = ipaddress - parent.save() - elif parent.oob_ip == ipaddress: - parent.oob_ip = None - parent.save() + # Assign/clear this IPAddress as the OOB for the associated Device + if type(interface) is Interface: + if self.cleaned_data['oob_for_parent']: + parent.oob_ip = ipaddress + update_fields.append('oob_ip') + elif parent.oob_ip == ipaddress: + parent.oob_ip = None + update_fields.append('oob_ip') + + # Save the parent object if appropriate. Update only the relevant fields to avoid conflicts with e.g. + # denormalized data on the parent object. + if update_fields: + parent.save(update_fields=update_fields) return ipaddress