Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions netbox/dcim/models/device_component_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class Meta:
),
)

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:
Expand All @@ -131,6 +137,11 @@ def to_objectchange(self, action):
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(
Expand Down
14 changes: 14 additions & 0 deletions netbox/dcim/models/device_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class Meta:
),
)

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})"
Expand All @@ -88,6 +94,14 @@ def to_objectchange(self, action):
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
Expand Down