Django import-export KeyError:u'id'

时间:2018-09-05 14:18:03

标签: jquery django python-3.x django-import-export

无法排除ID,甚至无法尝试使用stackoverflow中的现有问题:django-import-export: cannot exclude id field during import : KeyError: u'id'

resource.py

from import_export import resources
from .models import Bugdata

class BugDataResource(resources.ModelResource):

    class Meta:
        model = Bugdata
        skip_unchanged = True
        report_skipped = True
        exclude = ('id',)
        import_id_fields = ['Created_date', 'BugID', 'ID_Link', 'Summary', 'Status', 'Severity', 'Tags']

models.py

from django.db import models

class Bugdata(models.Model):
    Created_date = models.DateField()
    BugID = models.CharField(max_length=50,primary_key=True)
    ID_Link = models.URLField(max_length=300)
    Summary = models.CharField(max_length=600)
    Status = models.CharField(max_length=10)
    Severity = models.CharField(max_length=10)
    Tags = models.CharField(max_length=10)

    def __str__(self):
        return self.Summary

admin.py

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import Bugdata
# Register your models here.

@admin.register(Bugdata)
class NewtaskAdmin(ImportExportModelAdmin):
    pass

追踪

Error:
  Traceback (most recent call last):
  File "C:\Users\gomathis\AppData\Local\Programs\Python\Python36-32\lib\site- 
  packages\django_import_export-1.0.1-py3.6.egg\import_export\resources.py", 
  line 453, in import_row
  instance, new = self.get_or_init_instance(instance_loader, row)
  File "C:\Users\gomathis\AppData\Local\Programs\Python\Python36-32\lib\site- 
  packages\django_import_export-1.0.1-py3.6.egg\import_export\resources.py", 
  line 267, in get_or_init_instance
  instance = self.get_instance(instance_loader, row)
  File "C:\Users\gomathis\AppData\Local\Programs\Python\Python36-32\lib\site- 
  packages\django_import_export-1.0.1-py3.6.egg\import_export\resources.py", 
  line 261, in get_instance
  return instance_loader.get_instance(row)
  File "C:\Users\gomathis\AppData\Local\Programs\Python\Python36-32\lib\site- 
  packages\django_import_export-1.0.1- 
  py3.6.egg\import_export\instance_loaders.py", line 31, in get_instance
  field = self.resource.fields[key]
  KeyError: 'id'

1 个答案:

答案 0 :(得分:0)

显然我没有添加评论的名声,您解决了吗?我在使用与您自己相同的设置时发生了类似的情况,因此建议您仔细检查您要锁定的文件的编码。在Mac上将Excel保存为CSV格式时遇到了问题,在这种情况下,我使用了sublime之类的文本编辑器来确保将其保存为UTF8格式,并且该特定错误似乎消失了。