Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - django-import-export: cannot exclude id field during import : KeyError: u'id'

On Django-1.9.6, django-import-export-0.5

When I try to upload CSV without "id" field throws this error.

Line number: 1 - u'id'
13173474, Harry McDade, [email protected]
Traceback (most recent call last):
File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 434, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 258, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 252, in get_instance
return instance_loader.get_instance(row)
File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/instance_loaders.py", line 31, in get_instance
field = self.resource.fields[key]
KeyError: u'id'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Seems "django-import-export" still using the default one 'id'. for the time been, included a column 'id' in the header in CSV file and excluded importing in resources.py

class edxUserResource(resources.ModelResource):

    class Meta:
        model = edxUser
        skip_unchanged = True
        report_skipped = True
        exclude = ('id',)
        import_id_fields = ('edx_id', 'edx_email', 'edx_name',)
        #export_order = ('edx_id', 'edx_email')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...