diff --git a/netbox/dcim/forms/models.py b/netbox/dcim/forms/models.py index 6db3e263458..76292465344 100644 --- a/netbox/dcim/forms/models.py +++ b/netbox/dcim/forms/models.py @@ -1377,10 +1377,11 @@ class InventoryItemForm(CustomFieldModelForm): queryset=ContentType.objects.all(), limit_choices_to=MODULAR_COMPONENT_MODELS, required=False, - widget=StaticSelect + widget=forms.HiddenInput ) component_id = forms.IntegerField( - required=False + required=False, + widget=forms.HiddenInput ) tags = DynamicModelMultipleChoiceField( queryset=Tag.objects.all(), @@ -1396,7 +1397,6 @@ class Meta: fieldsets = ( ('Inventory Item', ('device', 'parent', 'name', 'label', 'role', 'description', 'tags')), ('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')), - ('Component', ('component_type', 'component_id')), ) widgets = { 'device': forms.HiddenInput(), diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index 4eda4a9377a..0c3a5f6a1f7 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -781,6 +781,8 @@ class InventoryItemTable(DeviceComponentTable): linkify=True ) component = tables.Column( + accessor=Accessor('component'), + orderable=False, linkify=True ) discovered = BooleanColumn() @@ -792,8 +794,8 @@ class InventoryItemTable(DeviceComponentTable): class Meta(BaseTable.Meta): model = InventoryItem fields = ( - 'pk', 'id', 'name', 'device', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag', - 'component', 'description', 'discovered', 'tags', + 'pk', 'id', 'name', 'device', 'component', 'label', 'role', 'manufacturer', 'part_id', 'serial', + 'asset_tag', 'description', 'discovered', 'tags', ) default_columns = ( 'pk', 'name', 'device', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag', diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index bee7f9ef090..81f45f7d062 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -2399,6 +2399,18 @@ class InventoryItemEditView(generic.ObjectEditView): class InventoryItemCreateView(generic.ComponentCreateView): queryset = InventoryItem.objects.all() model_form = forms.InventoryItemForm + template_name = 'dcim/inventoryitem_create.html' + + def alter_object(self, instance, request): + # Set component (if any) + component_type = request.GET.get('component_type') + component_id = request.GET.get('component_id') + + if component_type and component_id: + content_type = get_object_or_404(ContentType, pk=component_type) + instance.component = get_object_or_404(content_type.model_class(), pk=component_id) + + return instance class InventoryItemDeleteView(generic.ObjectDeleteView): diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index 577dfa4bf88..3f5b1465893 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -689,6 +689,9 @@ class ComponentCreateView(GetReturnURLMixin, ObjectPermissionRequiredMixin, View def get_required_permission(self): return get_permission_for_model(self.queryset.model, 'add') + def alter_object(self, instance, request): + return instance + def initialize_forms(self, request): data = request.POST if request.method == 'POST' else None initial_data = normalize_querydict(request.GET) @@ -704,9 +707,10 @@ def initialize_forms(self, request): def get(self, request): form, model_form = self.initialize_forms(request) + instance = self.alter_object(self.queryset.model, request) return render(request, self.template_name, { - 'obj': self.queryset.model, + 'obj': instance, 'obj_type': self.queryset.model._meta.verbose_name, 'replication_form': form, 'form': model_form, @@ -715,6 +719,7 @@ def get(self, request): def post(self, request): form, model_form = self.initialize_forms(request) + instance = self.alter_object(self.queryset.model, request) self.validate_form(request, form) @@ -725,7 +730,7 @@ def post(self, request): return redirect(self.get_return_url(request)) return render(request, self.template_name, { - 'obj': self.queryset.model, + 'obj': instance, 'obj_type': self.queryset.model._meta.verbose_name, 'replication_form': form, 'form': model_form, diff --git a/netbox/templates/dcim/inventoryitem_create.html b/netbox/templates/dcim/inventoryitem_create.html new file mode 100644 index 00000000000..ef20a218819 --- /dev/null +++ b/netbox/templates/dcim/inventoryitem_create.html @@ -0,0 +1,18 @@ +{% extends 'dcim/component_create.html' %} +{% load helpers %} + +{% block form %} + {% if obj.component %} +