mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-20 10:16:42 -06:00
Allow re-assigning inventoryitems to other devices
This commit is contained in:
parent
860805ba82
commit
41f631b65b
@ -1610,6 +1610,13 @@ class InventoryItemForm(DeviceComponentForm):
|
|||||||
('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')),
|
('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Specifically allow editing the device of IntentoryItems
|
||||||
|
if self.instance.pk:
|
||||||
|
self.fields['device'].disabled = False
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = InventoryItem
|
model = InventoryItem
|
||||||
fields = [
|
fields = [
|
||||||
|
@ -1153,3 +1153,20 @@ class InventoryItem(MPTTModel, ComponentModel):
|
|||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
"parent": "Cannot assign self as parent."
|
"parent": "Cannot assign self as parent."
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# Validation for moving InventoryItems
|
||||||
|
if self.pk:
|
||||||
|
# Cannot move an InventoryItem to another device if it has a parent
|
||||||
|
if self.parent and self.parent.device != self.device:
|
||||||
|
raise ValidationError({
|
||||||
|
"parent": "Parent inventory item does not belong to the same device."
|
||||||
|
})
|
||||||
|
|
||||||
|
# Prevent moving InventoryItems with children
|
||||||
|
first_child = self.get_children().first()
|
||||||
|
if first_child and first_child.device != self.device:
|
||||||
|
raise ValidationError("Cannot move an InventoryItem with dependent children")
|
||||||
|
|
||||||
|
# When moving an InventoryItem to another device, remove any associated component
|
||||||
|
if self.component and self.component.device != self.device:
|
||||||
|
self.component = None
|
Loading…
Reference in New Issue
Block a user