Просмотр исходного кода

Fixes #6936: Add missing parent column to inventory item import form

jeremystretch 4 лет назад
Родитель
Сommit
35b8fc6e83
3 измененных файлов с 26 добавлено и 4 удалено
  1. 1 0
      docs/release-notes/version-2.11.md
  2. 21 0
      netbox/dcim/forms.py
  3. 4 4
      netbox/dcim/tests/test_views.py

+ 1 - 0
docs/release-notes/version-2.11.md

@@ -14,6 +14,7 @@
 * [#6908](https://github.com/netbox-community/netbox/issues/6908) - Allow assignment of scope to VLAN groups upon import
 * [#6909](https://github.com/netbox-community/netbox/issues/6909) - Remove extraneous `site` column from VLAN group import form
 * [#6910](https://github.com/netbox-community/netbox/issues/6910) - Fix exception on invalid CSV import column name
+* [#6936](https://github.com/netbox-community/netbox/issues/6936) - Add missing `parent` column to inventory item import form
 
 ---
 

+ 21 - 0
netbox/dcim/forms.py

@@ -3852,11 +3852,32 @@ class InventoryItemCSVForm(CustomFieldModelCSVForm):
         to_field_name='name',
         required=False
     )
+    parent = CSVModelChoiceField(
+        queryset=Device.objects.all(),
+        to_field_name='name',
+        required=False,
+        help_text='Parent inventory item'
+    )
 
     class Meta:
         model = InventoryItem
         fields = InventoryItem.csv_headers
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+        # Limit parent choices to inventory items belonging to this device
+        device = None
+        if self.is_bound and 'device' in self.data:
+            try:
+                device = self.fields['device'].to_python(self.data['device'])
+            except forms.ValidationError:
+                pass
+        if device:
+            self.fields['parent'].queryset = InventoryItem.objects.filter(device=device)
+        else:
+            self.fields['parent'].queryset = InventoryItem.objects.none()
+
 
 class InventoryItemBulkCreateForm(
     form_from_model(InventoryItem, ['manufacturer', 'part_id', 'serial', 'asset_tag', 'discovered']),

+ 4 - 4
netbox/dcim/tests/test_views.py

@@ -1736,10 +1736,10 @@ class InventoryItemTestCase(ViewTestCases.DeviceComponentViewTestCase):
         }
 
         cls.csv_data = (
-            "device,name",
-            "Device 1,Inventory Item 4",
-            "Device 1,Inventory Item 5",
-            "Device 1,Inventory Item 6",
+            "device,name,parent",
+            "Device 1,Inventory Item 4,Inventory Item 1",
+            "Device 1,Inventory Item 5,Inventory Item 2",
+            "Device 1,Inventory Item 6,Inventory Item 3",
         )