Skip to content

Fixes #11934 - Clear previous primary ip assignment on edit #11953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions netbox/ipam/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def clean(self):
selected_objects[1]: "An IP address can only be assigned to a single object."
})
elif selected_objects:
self._old_parent = self.instance.assigned_object.parent_object
self.instance.assigned_object = self.cleaned_data[selected_objects[0]]
else:
self.instance.assigned_object = None
Expand All @@ -483,10 +484,19 @@ def save(self, *args, **kwargs):
if type(interface) in (Interface, VMInterface):
parent = interface.parent_object
if self.cleaned_data['primary_for_parent']:
# Clear any existing primary IP assignment
if old_parent := getattr(self, '_old_parent', None):
if ipaddress.address.version == 4:
old_parent.primary_ip4 = None
else:
old_parent.primary_ip6 = None
old_parent.save()

if ipaddress.address.version == 4:
parent.primary_ip4 = ipaddress
else:
parent.primary_ip6 = ipaddress

parent.save()
elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
parent.primary_ip4 = None
Expand Down