Raise ValidationError if parent object form data is incomplete

This commit is contained in:
Jason Novinger 2025-04-08 13:13:48 -05:00
parent f53e3103ff
commit 2bc636f34d

View File

@ -612,12 +612,18 @@ class ServiceImportForm(NetBoxModelImportForm):
def clean(self): def clean(self):
super().clean() super().clean()
if (parent_ct := self.cleaned_data.get('parent_object_type')):
if (parent := self.cleaned_data.get('parent')): if (parent := self.cleaned_data.get('parent')):
self.cleaned_data['parent_object_id'] = parent.pk self.cleaned_data['parent_object_id'] = parent.pk
elif not parent and (parent_id := self.cleaned_data.get('parent_object_id')): elif (parent_id := self.cleaned_data.get('parent_object_id')):
ct = self.cleaned_data.get('parent_object_type') parent = parent_ct.model_class().objects.filter(id=parent_id).first()
parent = ct.model_class().objects.filter(id=parent_id).first()
self.cleaned_data['parent'] = parent self.cleaned_data['parent'] = parent
else:
# If a parent object type is passed and we've made it to here, then raise a validation
# error since an associated parent object or parent object id has nto be passed
raise forms.ValidationError(
_("One of parent or parent_object_id needs to be included with parent_object_type")
)
for ip_address in self.cleaned_data.get('ipaddresses', []): for ip_address in self.cleaned_data.get('ipaddresses', []):
if not ip_address.assigned_object or getattr(ip_address.assigned_object, 'parent_object') != parent: if not ip_address.assigned_object or getattr(ip_address.assigned_object, 'parent_object') != parent: