mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-15 03:32:53 -06:00
Fixes #950: Fix site_id error on child device import
This commit is contained in:
parent
6a2a2d5d11
commit
f661c233be
@ -680,13 +680,21 @@ class DeviceFromCSVForm(BaseDeviceFromCSVForm):
|
|||||||
|
|
||||||
|
|
||||||
class ChildDeviceFromCSVForm(BaseDeviceFromCSVForm):
|
class ChildDeviceFromCSVForm(BaseDeviceFromCSVForm):
|
||||||
parent = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name', required=False,
|
parent = FlexibleModelChoiceField(
|
||||||
error_messages={'invalid_choice': 'Parent device not found.'})
|
queryset=Device.objects.all(),
|
||||||
|
to_field_name='name',
|
||||||
|
required=False,
|
||||||
|
error_messages={
|
||||||
|
'invalid_choice': 'Parent device not found.'
|
||||||
|
}
|
||||||
|
)
|
||||||
device_bay_name = forms.CharField(required=False)
|
device_bay_name = forms.CharField(required=False)
|
||||||
|
|
||||||
class Meta(BaseDeviceFromCSVForm.Meta):
|
class Meta(BaseDeviceFromCSVForm.Meta):
|
||||||
fields = ['name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag',
|
fields = [
|
||||||
'parent', 'device_bay_name']
|
'name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag', 'parent',
|
||||||
|
'device_bay_name',
|
||||||
|
]
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
||||||
|
@ -975,25 +975,26 @@ class Device(CreatedUpdatedModel, CustomFieldModel):
|
|||||||
# Child devices cannot be assigned to a rack face/unit
|
# Child devices cannot be assigned to a rack face/unit
|
||||||
if self.device_type.is_child_device and self.face is not None:
|
if self.device_type.is_child_device and self.face is not None:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'face': "Child device types cannot be assigned to a rack face. This is an attribute of the parent "
|
'face': "Child device types cannot be assigned to a rack face. This is an attribute of the "
|
||||||
"device."
|
"parent device."
|
||||||
})
|
})
|
||||||
if self.device_type.is_child_device and self.position:
|
if self.device_type.is_child_device and self.position:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'position': "Child device types cannot be assigned to a rack position. This is an attribute of the "
|
'position': "Child device types cannot be assigned to a rack position. This is an attribute of "
|
||||||
"parent device."
|
"the parent device."
|
||||||
})
|
})
|
||||||
|
|
||||||
# Validate rack space
|
# Validate rack space
|
||||||
rack_face = self.face if not self.device_type.is_full_depth else None
|
rack_face = self.face if not self.device_type.is_full_depth else None
|
||||||
exclude_list = [self.pk] if self.pk else []
|
exclude_list = [self.pk] if self.pk else []
|
||||||
try:
|
try:
|
||||||
available_units = self.rack.get_available_units(u_height=self.device_type.u_height, rack_face=rack_face,
|
available_units = self.rack.get_available_units(
|
||||||
exclude=exclude_list)
|
u_height=self.device_type.u_height, rack_face=rack_face, exclude=exclude_list
|
||||||
|
)
|
||||||
if self.position and self.position not in available_units:
|
if self.position and self.position not in available_units:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'position': "U{} is already occupied or does not have sufficient space to accommodate a(n) {} "
|
'position': "U{} is already occupied or does not have sufficient space to accommodate a(n) "
|
||||||
"({}U).".format(self.position, self.device_type, self.device_type.u_height)
|
"{} ({}U).".format(self.position, self.device_type, self.device_type.u_height)
|
||||||
})
|
})
|
||||||
except Rack.DoesNotExist:
|
except Rack.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
@ -1034,8 +1035,8 @@ class Device(CreatedUpdatedModel, CustomFieldModel):
|
|||||||
self.device_type.device_bay_templates.all()]
|
self.device_type.device_bay_templates.all()]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update Rack assignment for any child Devices
|
# Update Site and Rack assignment for any child Devices
|
||||||
Device.objects.filter(parent_bay__device=self).update(rack=self.rack)
|
Device.objects.filter(parent_bay__device=self).update(site=self.site, rack=self.rack)
|
||||||
|
|
||||||
def to_csv(self):
|
def to_csv(self):
|
||||||
return csv_format([
|
return csv_format([
|
||||||
|
@ -763,9 +763,12 @@ class ChildDeviceBulkImportView(PermissionRequiredMixin, BulkImportView):
|
|||||||
default_return_url = 'dcim:device_list'
|
default_return_url = 'dcim:device_list'
|
||||||
|
|
||||||
def save_obj(self, obj):
|
def save_obj(self, obj):
|
||||||
# Inherent rack from parent device
|
|
||||||
|
# Inherit site and rack from parent device
|
||||||
|
obj.site = obj.parent_bay.device.site
|
||||||
obj.rack = obj.parent_bay.device.rack
|
obj.rack = obj.parent_bay.device.rack
|
||||||
obj.save()
|
obj.save()
|
||||||
|
|
||||||
# Save the reverse relation
|
# Save the reverse relation
|
||||||
device_bay = obj.parent_bay
|
device_bay = obj.parent_bay
|
||||||
device_bay.installed_device = obj
|
device_bay.installed_device = obj
|
||||||
|
Loading…
Reference in New Issue
Block a user