7961 change to id, add docs

This commit is contained in:
Arthur 2022-10-19 14:11:43 -07:00
parent 1ecf51e492
commit 9e9b6c7da3
3 changed files with 6 additions and 4 deletions

View File

@ -20,12 +20,14 @@ To create a new object in NetBox, find the object type in the navigation menu an
## Bulk Import (CSV/YAML) ## 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.) 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.)
<!-- TODO: Screenshot --> <!-- TODO: Screenshot -->
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. 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 ## Scripting

View File

@ -334,7 +334,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
new_objs = [] new_objs = []
for row, data in enumerate(records, start=1): 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) obj_form = self.model_form(data, headers=headers, instance=obj)
# The form should only contain fields that are in the CSV # 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. # Iterate through CSV data and bind each row to a new model form instance.
with transaction.atomic(): with transaction.atomic():
headers, records = self._get_records(form, request) headers, records = self._get_records(form, request)
if "pk" in headers: if "id" in headers:
new_objs = self._update_objects(form, request, headers, records) new_objs = self._update_objects(form, request, headers, records)
else: else:
new_objs = self._create_objects(form, request, headers, records) new_objs = self._create_objects(form, request, headers, records)

View File

@ -222,7 +222,7 @@ def validate_csv(headers, fields, required_fields):
# Validate provided column headers # Validate provided column headers
is_update = False is_update = False
for field, to_field in headers.items(): for field, to_field in headers.items():
if field == "pk": if field == "id":
is_update = True is_update = True
continue continue
if field not in fields: if field not in fields: