Fix test to check for PK

This commit is contained in:
Daniel Sheppard 2023-12-21 14:39:56 -06:00
parent 9374890724
commit ec58b5338f

View File

@ -369,17 +369,18 @@ class IPAddressForm(TenancyForm, NetBoxModelForm):
'primary_for_parent', _("Only IP addresses assigned to an interface can be designated as primary IPs.") 'primary_for_parent', _("Only IP addresses assigned to an interface can be designated as primary IPs.")
) )
prev_interface = self.instance.interface.first() or self.instance.vminterface.first() if self.instance.pk:
# If the prev interface exists and does not match the new interface, we need to validate it isn't set as primary prev_interface = self.instance.interface.first() or self.instance.vminterface.first()
# for the parent # If the prev interface exists and does not match the new interface, we need to validate it isn't set as primary
if prev_interface and prev_interface != interface: # for the parent
prev_parent = prev_interface.device or prev_interface.virtual_machine if prev_interface and prev_interface != interface:
# Check that the parent exists and if it is set as a primary ip. prev_parent = prev_interface.device or prev_interface.virtual_machine
if prev_parent and prev_parent.primary_ip4 == self.instance or prev_parent.primary_ip6 == self.instance: # Check that the parent exists and if it is set as a primary ip.
self.add_error( if prev_parent and prev_parent.primary_ip4 == self.instance or prev_parent.primary_ip6 == self.instance:
selected_objects[0], self.add_error(
_("Cannot reassign IP address while it is designated as the primary IP for the parent object") selected_objects[0],
) _("Cannot reassign IP address while it is designated as the primary IP for the parent object")
)
# Do not allow assigning a network ID or broadcast address to an interface. # Do not allow assigning a network ID or broadcast address to an interface.
if interface and (address := self.cleaned_data.get('address')): if interface and (address := self.cleaned_data.get('address')):