gpt4 book ai didi

python - 没有id无法导入

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:29 25 4
gpt4 key购买 nike

软件:
Python3.6.2
Django1.11
django-导入-导出==0.5.1
OSX10.12.6

已安装的应用程序:

THIRD_PARTY_APPS = [
...,
'reversion', # django-reversion
'import_export', # django-import-export
]

要求:
1.模型有reversion
2.模型可以导入
3. 每个导入的角色都有上传者 created_userupdated_user(但是,现在我让它从 Excel 文件中读取,这就是问题所在)

尝试:
1. 由于其中 2 个要求将受到严格的 meta 类的严重影响。然后我应用 reichert 解决方案 https://github.com/etianen/django-reversion/issues/323
2.enter image description here enter image description here

问题:

Line number: 1 - get() keywords must be strings
None, 696 02, Lane, Ronald Regan, Ratíškovice, n/a, Dolichitus patagonum, admin, admin
Traceback (most recent call last):
File "/Users/el/.pyenv/versions/soken/lib/python3.6/site-packages/import_export/resources.py", line 434, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/Users/el/.pyenv/versions/soken/lib/python3.6/site-packages/import_export/resources.py", line 258, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/Users/el/.pyenv/versions/soken/lib/python3.6/site-packages/import_export/resources.py", line 252, in get_instance
return instance_loader.get_instance(row)
File "/Users/el/.pyenv/versions/soken/lib/python3.6/site-packages/import_export/instance_loaders.py", line 33, in get_instance
return self.get_queryset().get(**params)
TypeError: get() keywords must be strings

问题:
接下来我应该做什么?

型号:
https://gist.github.com/elcolie/98e641d57c5de3394f816a5d9b80ef13

admin.py
https://gist.github.com/elcolie/081017adc7b5e2fd7a3a38b0573597c6

引用文献:
Dealing with import of foreignKeys in django-import-export
django-import-export to export User model
Django Import/Export to multiple Models (foreignkey)

最佳答案

解决方案是我必须将 id 字段公开给文件:(

ZIPCODE_BASIC_FIELDS = [
'id', # Add
'zipcode',
'region',
'prefecture',
'city',
'town',
'building',
]

class ZIPCodeAddressResource(resources.ModelResource):
zipcode = fields.Field(widget=widgets.CharWidget())
created_user = fields.Field(column_name='created_user', attribute='created_user', widget=widgets.ForeignKeyWidget(User, 'username'))
updated_user = fields.Field(column_name='updated_user', attribute='updated_user', widget=widgets.ForeignKeyWidget(User, 'username'))

class Meta:
model = ZIPCodeAddressImportExport
# exclude = ['id', ] # Need to remove this line
fields = IMPORT_COLUMNS
skip_unchanged = True
report_skipped = True
import_id_fields = ['id', ] # Let it had 'id'
export_order = IMPORT_COLUMNS

关于python - 没有id无法导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46524433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com