From 9e9b6c7da3cbfd72ba04a0708caf887424b45722 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 19 Oct 2022 14:11:43 -0700 Subject: [PATCH] 7961 change to id, add docs --- docs/getting-started/populating-data.md | 4 +++- netbox/netbox/views/generic/bulk_views.py | 4 ++-- netbox/utilities/forms/utils.py | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/getting-started/populating-data.md b/docs/getting-started/populating-data.md index bb0e8e17f..9a2386d71 100644 --- a/docs/getting-started/populating-data.md +++ b/docs/getting-started/populating-data.md @@ -20,12 +20,14 @@ To create a new object in NetBox, find the object type in the navigation menu an ## Bulk Import (CSV/YAML) -NetBox supports the bulk import of new objects using CSV-formatted data. This method can be ideal for importing spreadsheet data, which is very easy to convert to CSV data. CSV data can be imported either as raw text using the form field, or by uploading a properly formatted CSV file. +NetBox supports the bulk import of new objects, and updating of existing objects using CSV-formatted data. This method can be ideal for importing spreadsheet data, which is very easy to convert to CSV data. CSV data can be imported either as raw text using the form field, or by uploading a properly formatted CSV file. When viewing the CSV import form for an object type, you'll notice that the headers for the required columns have been pre-populated. Each form has a table beneath it titled "CSV Field Options," which lists _all_ supported columns for your reference. (Generally, these map to the fields you see in the corresponding creation form for individual objects.) +If an "id" field is added the data will be used to update existing records instead of importing new objects. + Note that some models (namely device types and module types) do not support CSV import. Instead, they accept YAML-formatted data to facilitate the import of both the parent object as well as child components. ## Scripting diff --git a/netbox/netbox/views/generic/bulk_views.py b/netbox/netbox/views/generic/bulk_views.py index b9350518e..4a1c867fe 100644 --- a/netbox/netbox/views/generic/bulk_views.py +++ b/netbox/netbox/views/generic/bulk_views.py @@ -334,7 +334,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): new_objs = [] for row, data in enumerate(records, start=1): - obj = self.queryset.model.objects.get(pk=data["pk"]) + obj = self.queryset.model.objects.get(pk=data["id"]) obj_form = self.model_form(data, headers=headers, instance=obj) # The form should only contain fields that are in the CSV @@ -405,7 +405,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): # Iterate through CSV data and bind each row to a new model form instance. with transaction.atomic(): headers, records = self._get_records(form, request) - if "pk" in headers: + if "id" in headers: new_objs = self._update_objects(form, request, headers, records) else: new_objs = self._create_objects(form, request, headers, records) diff --git a/netbox/utilities/forms/utils.py b/netbox/utilities/forms/utils.py index 48a68f13e..1a2f62b2e 100644 --- a/netbox/utilities/forms/utils.py +++ b/netbox/utilities/forms/utils.py @@ -222,7 +222,7 @@ def validate_csv(headers, fields, required_fields): # Validate provided column headers is_update = False for field, to_field in headers.items(): - if field == "pk": + if field == "id": is_update = True continue if field not in fields: