diff --git a/netbox/dcim/models/device_component_templates.py b/netbox/dcim/models/device_component_templates.py index 3b136987d..6ff58b0f0 100644 --- a/netbox/dcim/models/device_component_templates.py +++ b/netbox/dcim/models/device_component_templates.py @@ -120,6 +120,12 @@ class ModularComponentTemplateModel(ComponentTemplateModel): ), ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Cache the original DeviceType ID for reference under clean() + self._original_device_type = self.device_type_id + def to_objectchange(self, action): objectchange = super().to_objectchange(action) if self.device_type is not None: @@ -131,6 +137,11 @@ class ModularComponentTemplateModel(ComponentTemplateModel): def clean(self): super().clean() + if self.pk is not None and self._original_device_type != self.device_type_id: + raise ValidationError({ + "device_type": "Component templates cannot be moved to a different device type." + }) + # A component template must belong to a DeviceType *or* to a ModuleType if self.device_type and self.module_type: raise ValidationError( diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index b879b77d3..c30ce3a97 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -78,6 +78,12 @@ class ComponentModel(NetBoxModel): ), ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Cache the original Device ID for reference under clean() + self._original_device = self.device_id + def __str__(self): if self.label: return f"{self.name} ({self.label})" @@ -88,6 +94,14 @@ class ComponentModel(NetBoxModel): objectchange.related_object = self.device return objectchange + def clean(self): + super().clean() + + if self.pk is not None and self._original_device != self.device_id: + raise ValidationError({ + "device": "Components cannot be moved to a different device." + }) + @property def parent_object(self): return self.device