2

在 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'
4

3 回答 3

7

似乎“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')
于 2017-07-11T08:14:17.230 回答
1

id 字段是一个自动增量字段,因此如果您要添加新记录(我相信这是大多数情况),id 字段应该在标题中(csv 文件的第一行),其余行应该包含空我想要 : ,

例子:

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

这在大多数情况下应该没问题。

于 2018-07-06T15:22:52.460 回答
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']
于 2018-12-05T18:59:07.067 回答