【问题标题】:Ids incremented twice during import-export (Django)在导入导出期间,ID 增加了两次(Django)
【发布时间】:2021-08-09 05:22:18
【问题描述】:
我正在使用 Django-import-export 库,在上传 json/csv 文件中的数据时遇到问题。当我通过 django-admin 上传数据文件时,id 字段增加了两倍?如何解决这个问题呢?谢谢
【问题讨论】:
标签:
django
django-models
django-views
django-templates
【解决方案1】:
你想像这样使用admin resource
class MyModelResource(resources.ModelResource):
class Meta:
model = MyModel
import_id_fields = ['id', or 'ID' or whatever it is in your csv file column header. Or comment out this option if your import file has no column header for id]
#skip_unchanged = True
#report_skipped = True
然后在您的模型的管理类中:
@admin.register(MyModel)
class MyModelAdmin(ImportExportModelAdmin):
resource_class = MyModelResource
您可能还想尝试IMPORT_EXPORT_USE_TRANSACTIONS 设置。