1

软件: Python3.6.2 Django1.11
django -import-export==0.5.1 OSX10.12.6



安装_应用程序:

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

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

尝试:
1.由于要求中的2个将受到严格的meta班级的严厉打击。然后我应用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 中的 foreignKeys 导入
django-import-export 导出用户模型
Django Import/Export to multiple Models (foreignkey)

4

2 回答 2

1

解决方案是我必须向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
于 2017-10-02T11:40:53.457 回答
0

请参阅此答案,它将排除您的 ID,如果您想 在 Django import-export Csv 中从导入中排除 id,您还可以包含任何外键

于 2018-12-05T19:14:10.990 回答