django-import-export:导入时无法排除id字段:KeyError:u'id'

时间:2017-07-11 08:14:17

标签: python django django-import-export

在Django-1.9.6上,django-import-export-0.5

当我尝试上传没有“id”字段的CSV时,会抛出此错误。

Line number: 1 - u'id'
13173474, Harry McDade, 10.harry.asas@asasasas.com
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'

3 个答案:

答案 0 :(得分:1)

似乎“django-import-export”仍然使用默认的'id'。当时,在CSV文件的标题中包含一个列'id',并在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')

答案 1 :(得分:0)

id字段是一个自动递增字段,因此,如果要添加新记录(我相信是最常见的情况),则id字段应该位于标题(csv文件的第一行)和其余各行中应该包含空ID,如:

示例:

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')

这在大多数情况下都可以。

答案 2 :(得分:0)

我已经解决了导入不带ID列的解决方案 这是代码,看看

    if request.method == 'POST':
    queryset = Client.objects.filter(company=company)
    company = Company.objects.get(id=company)
    person_resource = ClientResource()
    dataset = Dataset()
    new_persons = request.FILES['myfile']
    imported_data = dataset.load(new_persons.read().decode('utf-8'), format='csv')
    try:
        for row in dataset:
            client = Client()
            client.company = company
            client.title = row[0]
            client.first_name = row[1]
            client.last_name = row[2]
            client.email = row[3]
            client.position = row[4]
            client.company_name = row[5]
            client.vat_number = row[6]
            client.website = row[7]
            client.address = row[8]
            client.city = row[9]
            client.state = row[10]
            client.zip = row[11]
            client.country = row[12]
            client.phone = row[13]
            client.fax = row[14]
            client.notes = row[15]
            client.save()

    except Client.DoesNotExist:
        raise Http404("There is a Problem with The CSV")

return render(request, 'import.html')

我的资源看起来像这样

class ClientResource(resources.ModelResource):
company = fields.Field(
    column_name='company',
    attribute='company',
    widget=ForeignKeyWidget(Company, 'name'))

class Meta:
    model = Client
    skip_unchanged = True
    report_skipped = True
    exclude = ('id', 'company', 'status', 'modified', 'created')
    import_id_fields = ['email']