diff --git a/netbox/ipam/forms.py b/netbox/ipam/forms.py index 78deadd45f3..a5c3923a57f 100644 --- a/netbox/ipam/forms.py +++ b/netbox/ipam/forms.py @@ -641,16 +641,23 @@ def save(self, *args, **kwargs): # Set interface if self.cleaned_data['device'] and self.cleaned_data['interface_name']: - self.instance.interface = Interface.objects.get(device=self.cleaned_data['device'], - name=self.cleaned_data['interface_name']) + self.instance.interface = Interface.objects.get( + device=self.cleaned_data['device'], + name=self.cleaned_data['interface_name'] + ) + + ipaddress = super(IPAddressCSVForm, self).save(*args, **kwargs) + # Set as primary for device if self.cleaned_data['is_primary']: + device = self.cleaned_data['device'] if self.instance.address.version == 4: - self.instance.primary_ip4_for = self.cleaned_data['device'] + device.primary_ip4 = ipaddress elif self.instance.address.version == 6: - self.instance.primary_ip6_for = self.cleaned_data['device'] + device.primary_ip6 = ipaddress + device.save() - return super(IPAddressCSVForm, self).save(*args, **kwargs) + return ipaddress class IPAddressBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm): diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 3cc54d2523e..74432d1802d 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -665,19 +665,6 @@ class IPAddressBulkImportView(PermissionRequiredMixin, BulkImportView): table = tables.IPAddressTable default_return_url = 'ipam:ipaddress_list' - def save_obj(self, obj): - obj.save() - - # Update primary IP for device if needed. The Device must be updated directly in the database; otherwise we risk - # overwriting a previous IP assignment from the same import (see #861). - try: - if obj.family == 4 and obj.primary_ip4_for: - Device.objects.filter(pk=obj.primary_ip4_for.pk).update(primary_ip4=obj) - elif obj.family == 6 and obj.primary_ip6_for: - Device.objects.filter(pk=obj.primary_ip6_for.pk).update(primary_ip6=obj) - except Device.DoesNotExist: - pass - class IPAddressBulkEditView(PermissionRequiredMixin, BulkEditView): permission_required = 'ipam.change_ipaddress'