使用 django import_export 导入没有 id 字段作为主键的模型
Import model without id field as primary key with django import_export
刚刚尝试了 django-import-export 从 csv 文件中导入一些东西。
按照文档进行操作,但在尝试导入以下模型时总是出现错误:
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)
然后资源 class:
class TokenResource(resources.ModelResource):
class Meta:
model = Token
skip_unchanged = True
report_skipped = True
fields = "key"
现在导入 csv 文件时出现以下错误:
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'
我的模型中没有字段 id,主键字段是关键,为什么不使用它?
或者模型必须有一个 id 字段才能导入?我知道这个 ids 是用来做比较等等的,但是模型中有一个主键字段,所以我不明白为什么不这样做。
如何在不重命名我的模型的情况下更改它?我在文档中找不到提示。
谢谢
There is no field id within my model the primary key field is key so why it is not taken?
用于识别对象是否存在的默认字段称为'id'。但是,您可以使用 import_id_fields
覆盖,因此在您的情况下,以下内容应该有效:
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",)
刚刚尝试了 django-import-export 从 csv 文件中导入一些东西。 按照文档进行操作,但在尝试导入以下模型时总是出现错误:
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)
然后资源 class:
class TokenResource(resources.ModelResource):
class Meta:
model = Token
skip_unchanged = True
report_skipped = True
fields = "key"
现在导入 csv 文件时出现以下错误:
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'
我的模型中没有字段 id,主键字段是关键,为什么不使用它?
或者模型必须有一个 id 字段才能导入?我知道这个 ids 是用来做比较等等的,但是模型中有一个主键字段,所以我不明白为什么不这样做。
如何在不重命名我的模型的情况下更改它?我在文档中找不到提示。
谢谢
There is no field id within my model the primary key field is key so why it is not taken?
用于识别对象是否存在的默认字段称为'id'。但是,您可以使用 import_id_fields
覆盖,因此在您的情况下,以下内容应该有效:
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",)