没有id无法导入
Can not import without id
软件:
Python3.6.2
Django1.11
django-导入-导出==0.5.1
OSX10.12.6
INSTALLED_APPS:
THIRD_PARTY_APPS = [
...,
'reversion', # django-reversion
'import_export', # django-import-export
]
要求:
1.模型有reversion
2.模型可以做到import
3. 每个导入角色的上传者为created_user
,updated_user
(但是,现在我让它从 Excel 文件中读取,这就是问题所在)
尝试次数:
1. 由于 strict meta
class 会严重影响其中 2 个要求。然后我应用 reichert
解决方案 https://github.com/etianen/django-reversion/issues/323
2.
问题:
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
参考文献:
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
查看此答案,它将排除您的 ID,如果您愿意,也可以包含任何外键
软件:
Python3.6.2
Django1.11
django-导入-导出==0.5.1
OSX10.12.6
INSTALLED_APPS:
THIRD_PARTY_APPS = [
...,
'reversion', # django-reversion
'import_export', # django-import-export
]
要求:
1.模型有reversion
2.模型可以做到import
3. 每个导入角色的上传者为created_user
,updated_user
(但是,现在我让它从 Excel 文件中读取,这就是问题所在)
尝试次数:
1. 由于 strict meta
class 会严重影响其中 2 个要求。然后我应用 reichert
解决方案 https://github.com/etianen/django-reversion/issues/323
2.
问题:
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
参考文献:
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
查看此答案,它将排除您的 ID,如果您愿意,也可以包含任何外键