mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Fixes: #18833 Inventory Item Bulk Import - 'InventoryItemImportForm' has no field named 'component_id'. (#18874)
* Refactor InventoryItemImportForm clean method * Add super().clean(); renamed content_type; simplified component creation * Fix missing component_name issue * Update netbox/dcim/forms/bulk_import.py --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
parent
c50b1c989d
commit
bf1a9a6e2d
@ -1161,27 +1161,45 @@ class InventoryItemImportForm(NetBoxModelImportForm):
|
|||||||
else:
|
else:
|
||||||
self.fields['parent'].queryset = InventoryItem.objects.none()
|
self.fields['parent'].queryset = InventoryItem.objects.none()
|
||||||
|
|
||||||
def clean_component_name(self):
|
def clean(self):
|
||||||
content_type = self.cleaned_data.get('component_type')
|
super().clean()
|
||||||
component_name = self.cleaned_data.get('component_name')
|
cleaned_data = self.cleaned_data
|
||||||
|
component_type = cleaned_data.get('component_type')
|
||||||
|
component_name = cleaned_data.get('component_name')
|
||||||
device = self.cleaned_data.get("device")
|
device = self.cleaned_data.get("device")
|
||||||
|
|
||||||
if not device and hasattr(self, 'instance') and hasattr(self.instance, 'device'):
|
if component_type:
|
||||||
device = self.instance.device
|
if device is None:
|
||||||
|
cleaned_data.pop('component_type', None)
|
||||||
if not all([device, content_type, component_name]):
|
if component_name is None:
|
||||||
return None
|
cleaned_data.pop('component_type', None)
|
||||||
|
raise forms.ValidationError(
|
||||||
model = content_type.model_class()
|
_("Component name must be specified when component type is specified")
|
||||||
|
)
|
||||||
|
if all([device, component_name]):
|
||||||
try:
|
try:
|
||||||
component = model.objects.get(device=device, name=component_name)
|
model = component_type.model_class()
|
||||||
self.instance.component = component
|
self.instance.component = model.objects.get(device=device, name=component_name)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
|
cleaned_data.pop('component_type', None)
|
||||||
|
cleaned_data.pop('component_name', None)
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
_("Component not found: {device} - {component_name}").format(
|
_("Component not found: {device} - {component_name}").format(
|
||||||
device=device, component_name=component_name
|
device=device, component_name=component_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
cleaned_data.pop('component_type', None)
|
||||||
|
if not component_name:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_("Component name must be specified when component type is specified")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if component_name:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_("Component type must be specified when component name is specified")
|
||||||
|
)
|
||||||
|
return cleaned_data
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user