Skip to content

Commit dbfdf31

Browse files
authored
Closes #20459 : clean is_oob and is_primary on bulk_import (#20657)
1 parent 639bc44 commit dbfdf31

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

netbox/ipam/forms/bulk_import.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ def __init__(self, data=None, *args, **kwargs):
369369
**{f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data['virtual_machine']}
370370
)
371371

372+
def clean_is_primary(self):
373+
# Make sure is_primary is None when it's not included in the uploaded data
374+
if 'is_primary' not in self.data:
375+
return None
376+
else:
377+
return self.cleaned_data['is_primary']
378+
379+
def clean_is_oob(self):
380+
# Make sure is_oob is None when it's not included in the uploaded data
381+
if 'is_oob' not in self.data:
382+
return None
383+
else:
384+
return self.cleaned_data['is_oob']
385+
372386
def clean(self):
373387
super().clean()
374388

@@ -411,18 +425,18 @@ def save(self, *args, **kwargs):
411425
ipaddress = super().save(*args, **kwargs)
412426

413427
# Set as primary for device/VM
414-
if self.cleaned_data.get('is_primary'):
428+
if self.cleaned_data.get('is_primary') is not None:
415429
parent = self.cleaned_data.get('device') or self.cleaned_data.get('virtual_machine')
416430
if self.instance.address.version == 4:
417-
parent.primary_ip4 = ipaddress
431+
parent.primary_ip4 = ipaddress if self.cleaned_data.get('is_primary') else None
418432
elif self.instance.address.version == 6:
419-
parent.primary_ip6 = ipaddress
433+
parent.primary_ip6 = ipaddress if self.cleaned_data.get('is_primary') else None
420434
parent.save()
421435

422436
# Set as OOB for device
423-
if self.cleaned_data.get('is_oob'):
437+
if self.cleaned_data.get('is_oob') is not None:
424438
parent = self.cleaned_data.get('device')
425-
parent.oob_ip = ipaddress
439+
parent.oob_ip = ipaddress if self.cleaned_data.get('is_oob') else None
426440
parent.save()
427441

428442
return ipaddress

0 commit comments

Comments
 (0)