如何解决在Django导入导出中使用外键导入数据时出错
我已经使用django import-export库实现了导入数据。当我尝试使用不存在的对象名称导入数据时,会引发错误不匹配的查询。
Line number: 1 - CustomUser matching query does not exist.
sample@sample.com,data1,firstname,secondname
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/import_export/resources.py",line 520,in import_row
self.import_obj(instance,row,dry_run)
File "/usr/local/lib/python3.7/site-packages/import_export/resources.py",line 385,in import_obj
self.import_field(field,obj,data)
File "/usr/local/lib/python3.7/site-packages/import_export/resources.py",line 368,in import_field
field.save(obj,data,is_m2m)
File "/usr/local/lib/python3.7/site-packages/import_export/fields.py",line 110,in save
cleaned = self.clean(data)
File "/usr/local/lib/python3.7/site-packages/import_export/fields.py",line 66,in clean
value = self.widget.clean(value,row=data)
File "/usr/local/lib/python3.7/site-packages/import_export/widgets.py",line 364,in clean
return self.get_queryset(value,*args,**kwargs).get(**{self.field: val})
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py",line 408,in get
self.model._Meta.object_name
accounts.models.CustomUser.DoesNotExist: CustomUser matching query does not exist.
自定义资源导入类:
class UserResourceImport(resources.ModelResource):
""" Resource class for import utilities """
# set foreign key relations for model,this way we can provide name instead of id
custom_user = fields.Field(column_name='custom_user',attribute='custom_user',widget=ForeignKeyWidget(CustomUser,'name'))
class Meta:
model = user_model
fields = ('email','custom_user','first_name','last_name')
skip_unchanged = False
report_skipped = True
export_order = ('email','last_name')
import_id_fields = ['email']
其他所有导入错误都会导致生成以下模板: Normal error
有什么方法可以处理此错误,以便它不会将堆栈跟踪信息打印到导入模板中并跳过包含无效数据的行吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。