joe
joe

Reputation: 9494

Can not import without id

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

INSTALLED_APPS:

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

Requirements:
1. Model has reversion
2. Model can be able to do import
3. Each imported role has uploader as created_user, and updated_user (However, just for now I let it read from the Excel file and here it is the problem)

Attempts:
1. Since 2 of the requirements will hit hard with strict meta class. Then I apply reichert solution https://github.com/etianen/django-reversion/issues/323
2. enter image description here enter image description here

Problem:

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

Question:
What should I do next?

Models:
https://gist.github.com/elcolie/98e641d57c5de3394f816a5d9b80ef13

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

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

Upvotes: 1

Views: 1673

Answers (2)

Saad Mirza
Saad Mirza

Reputation: 1177

See this Answer, It will exclude your ID and also you can include any Foreign key if you want to Exclude id from import in Django import-export Csv

Upvotes: 0

joe
joe

Reputation: 9494

The solution is I have to expose id field to the file :(

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

Upvotes: 1

Related Questions