无身份

时间:2017-10-02 11:15:35

标签: python django django-import-export

软件:
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_userupdated_user (但是,就目前而言,我让它从Excel文件中读取,这是问题所在)

尝试:
1.由于严格的meta课程,其中2项要求会受到严重影响。然后我应用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)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

请参阅此答案,它将排除您的ID,并且如果您愿意,还可以包括任何外键 Exclude id from import in Django import-export Csv