Update only the primary/OOB IP fields when saving the parent object
Some checks are pending
CI / build (20.x, 3.12) (push) Waiting to run
CI / build (20.x, 3.13) (push) Waiting to run
CI / build (20.x, 3.14) (push) Waiting to run

This commit is contained in:
Jeremy Stretch
2026-01-19 11:57:35 -05:00
parent 5dd5d65d74
commit 78c56c2cb8

View File

@@ -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