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

后端 未结 3 1728
无人共我
无人共我 2020-12-18 17:15

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          


        
3条回答
  •  时光说笑
    2020-12-18 17:41

    The id field is an auto increment field, so if you're adding new records (I believe its the most case) the id field should be there in the header (first line of csv file) and the rest of the rows should contain empty id like : ,

    Example:

    CSV File:
    id, username,email,password
    ,ahmad,ahmad@all.com,secretum
    ,salafi,salafi@gmail.com,Passhdjdj
    
    In the Resource file (py):
    class JasResult(ImportExportModelAdmin):
        resource_class = JasResource
        skip_unchanged = True
        report_skipped = True
        exclude = ('id',)
        import_id_fields = ('username','email','password')
    

    This should fine for most cases.

提交回复
热议问题