just tried out django-import-export to import som things from a csv-file. Followed the doc but I always get an error when trying to import the following Model:
class Token(models.Model):
key = models.CharField(db_index=True,unique=True,primary_key=True, )
pool = models.ForeignKey(Pool, on_delete=models.CASCADE)
state = models.PositiveSmallIntegerField(default=State.VALID, choices=State.choices)
then the resource class:
class TokenResource(resources.ModelResource):
class Meta:
model = Token
skip_unchanged = True
report_skipped = True
fields = "key"
Now when importing a csv-file i get the following errors:
Error
row number: 1 - 'id'
Traceback (most recent call last):
File "/backend/.venv/lib/python3.9/site-packages/import_export/resources.py", line 667, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/backend/.venv/lib/python3.9/site-packages/import_export/resources.py", line 359, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/backend/.venv/lib/python3.9/site-packages/import_export/resources.py", line 346, in get_instance
import_id_fields = [
File "/backend/.venv/lib/python3.9/site-packages/import_export/resources.py", line 347, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
There is no field id within my model the primary key field is key so why it is not taken?
Or could it be that a model must have an id field to get imported? I know that this ids are taken for comparision and so on but there is a primary key field within the model, so I do not understand why this is not taken.
How could I change this without having to rename my model? I could not find a hint within the docs.
Thanks
There is no field id within my model the primary key field is key so why it is not taken?
The default field for identifying whether an object exists or not is called 'id'. However you can override using import_id_fields
, so in your case the following should work:
class TokenResource(resources.ModelResource):
class Meta:
model = Token
skip_unchanged = True
report_skipped = True
import_id_fields = ("key",)
# you can optionally use 'fields' as a whitelist for
# fields to be imported
fields = ("key",)