#4347: Changelog & cleanup

This commit is contained in:
jeremystretch
2022-11-10 11:05:44 -05:00
parent 93e7457e0d
commit 653acbf62c
2 changed files with 11 additions and 12 deletions

View File

@@ -375,19 +375,14 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
form.add_error('data', f"Row {i}: Object with ID {object_id} does not exist")
raise ValidationError('')
# Instantiate the model form for the object
model_form_kwargs = {
'data': record,
'instance': instance,
}
if form.cleaned_data['format'] == ImportFormatChoices.CSV:
model_form = self.model_form(record, instance=instance, headers=form._csv_headers)
else:
model_form = self.model_form(record, instance=instance)
# Assign default values for any fields which were not specified.
# We have to do this manually because passing 'initial=' to the form
# on initialization merely sets default values for the widgets.
# Since widgets are not used for YAML/JSON import, we first bind the
# imported data normally, then update the form's data with the applicable
# field defaults as needed prior to form validation.
for field_name, field in model_form.fields.items():
if field_name not in record and hasattr(field, 'initial'):
model_form.data[field_name] = field.initial
model_form_kwargs['headers'] = form._csv_headers
model_form = self.model_form(**model_form_kwargs)
# When updating, omit all form fields other than those specified in the record. (No
# fields are required when modifying an existing object.)