django-import-export: cannot exclude id field during import : KeyError: u'id'
django-import-export: cannot exclude id field during import : KeyError: u'id'
在 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'
似乎 "django-import-export" 仍在使用默认值 'id'。目前,在 CSV 文件的 header 中包含一列 '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')
id 字段是一个自动递增字段,所以如果您要添加新记录(我相信这是大多数情况)id 字段应该在 header(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')
大多数情况下这应该没问题。
我已经找到了导入没有 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']
在 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'
似乎 "django-import-export" 仍在使用默认值 'id'。目前,在 CSV 文件的 header 中包含一列 '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')
id 字段是一个自动递增字段,所以如果您要添加新记录(我相信这是大多数情况)id 字段应该在 header(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')
大多数情况下这应该没问题。
我已经找到了导入没有 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']