diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index 639f8aadb..4a93e4a80 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -1283,3 +1283,10 @@ class InventoryItem(MPTTModel, ComponentModel, TrackingModelMixin): raise ValidationError({ "device": _("Cannot assign inventory item to component on another device") }) + + @property + def children(self): + return InventoryItem.objects.filter( + device=self.device, + parent=self + ) diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index 7c75dd26e..6bd664104 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -2993,6 +2993,25 @@ class InventoryItemBulkDeleteView(generic.BulkDeleteView): template_name = 'dcim/inventoryitem_bulk_delete.html' +@register_model_view(InventoryItem, 'children') +class InventoryItemChildrenView(generic.ObjectChildrenView): + queryset = InventoryItem.objects.all() + child_model = InventoryItem + table = tables.InventoryItemTable + filterset = filtersets.InventoryItemFilterSet + template_name = 'generic/object_children.html' + tab = ViewTab( + label=_('Children'), + badge=lambda obj: obj.children.count(), + permission='dcim.view_inventoryitem', + hide_if_empty=True, + weight=5000 + ) + + def get_children(self, request, parent): + return parent.children.restrict(request.user, 'view') + + # # Inventory item roles #