I am building a tool that allows users to upload CSV files to update the database using the django-import-export tool. I uploaded my test CSV file with one data row, then uploaded it again and got a duplicated row (with a new primary key but all the other values are the same). The row.import_type value is "updated" but the only thing that is updated is the id.
Then I upload the same file a third time and get an error:
app.models.Role.MultipleObjectsReturned: get() returned more than one Role -- it returned 2!
(I really appreciate the exclamation point in that error message, by the way.)
Ideally I would get a skipped row on the second import and third import of the file. I suppose I'd be okay with an error. The file's contents are:
Sales Role,System Role,System Plan,id
Sales Rep,Account Executive,951M-NA,
This is the format users get when they export the csv dataset. Ideally they would export a file, change a few columns (aside from the name which is the import_id_field), and re-upload the data.
In app/resources.py:
class RoleResourec(resources.ModelResource):
name = Field(attribute='name', column_name="Sales Role")
default_role = Field(attribute='default_role', column_name="System Role")
default_plan = Field(attribute='default_plan', column_name="System Plan")
class Meta:
models=Role
fields= ('id', 'name', 'default_role', 'default_plan')
import_id_fields = ('name',)
skip_unchanged = True
From what I can tell, on the second import, the get_or_init_instance() method isn't finding the object from the first import, but then does find them on the third. I haven't done anything to the resource to customize the import workflow as described in Import data workflow page.
What's going wrong here? Do I need to customize the import workflow or did I miss yet-another required attribute in the Resource?