mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-20 10:38:44 -06:00
Update only the primary/OOB IP fields when saving the parent object
This commit is contained in:
@@ -424,34 +424,40 @@ class IPAddressForm(TenancyForm, PrimaryModelForm):
|
|||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
ipaddress = super().save(*args, **kwargs)
|
ipaddress = super().save(*args, **kwargs)
|
||||||
|
|
||||||
# Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
|
|
||||||
interface = self.instance.assigned_object
|
interface = self.instance.assigned_object
|
||||||
if type(interface) in (Interface, VMInterface):
|
if type(interface) in (Interface, VMInterface):
|
||||||
parent = interface.parent_object
|
parent = interface.parent_object
|
||||||
parent.snapshot()
|
parent.snapshot()
|
||||||
|
update_fields = []
|
||||||
|
|
||||||
|
# Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
|
||||||
if self.cleaned_data['primary_for_parent']:
|
if self.cleaned_data['primary_for_parent']:
|
||||||
if ipaddress.address.version == 4:
|
if ipaddress.address.version == 4:
|
||||||
parent.primary_ip4 = ipaddress
|
parent.primary_ip4 = ipaddress
|
||||||
|
update_fields.append('primary_ip4')
|
||||||
else:
|
else:
|
||||||
parent.primary_ip6 = ipaddress
|
parent.primary_ip6 = ipaddress
|
||||||
parent.save()
|
update_fields.append('primary_ip6')
|
||||||
elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
|
elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
|
||||||
parent.primary_ip4 = None
|
parent.primary_ip4 = None
|
||||||
parent.save()
|
update_fields.append('primary_ip4')
|
||||||
elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
|
elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
|
||||||
parent.primary_ip6 = None
|
parent.primary_ip6 = None
|
||||||
parent.save()
|
update_fields.append('primary_ip6')
|
||||||
|
|
||||||
# Assign/clear this IPAddress as the OOB for the associated Device
|
# Assign/clear this IPAddress as the OOB for the associated Device
|
||||||
if type(interface) is Interface:
|
if type(interface) is Interface:
|
||||||
parent = interface.parent_object
|
if self.cleaned_data['oob_for_parent']:
|
||||||
parent.snapshot()
|
parent.oob_ip = ipaddress
|
||||||
if self.cleaned_data['oob_for_parent']:
|
update_fields.append('oob_ip')
|
||||||
parent.oob_ip = ipaddress
|
elif parent.oob_ip == ipaddress:
|
||||||
parent.save()
|
parent.oob_ip = None
|
||||||
elif parent.oob_ip == ipaddress:
|
update_fields.append('oob_ip')
|
||||||
parent.oob_ip = None
|
|
||||||
parent.save()
|
# 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
|
return ipaddress
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user