django import-export 错误 `has_errors()` 是针对 Resource 还是整个模型验证?
我尝试按照这个链接的内容来做: https://github.com/bmihelac/django-import-export/blob/master/docs/getting_started.rst
我定义了资源,具体如下:
from import_export import resources
from core_backend.models.exhibitor import Exhibitor
from django.contrib.auth.models import User
from import_export import fields
class ExhibitorResource(resources.ModelResource):
email = fields.Field()
class Meta:
model = Exhibitor
fields = ('id', 'stand_id', 'email', 'title')
def dehydrate_email(self, exhibitor):
email = User.objects.filter(id=exhibitor.user_id)[0].email
return email
然后在命令行中我尝试:
import tablib
from import_export import resources
from core_backend.models.exhibitor import Exhibitor
from core_backend.models.exhibitor_resource import ExhibitorResource
my_res = ExhibitorResource()
my_res.fields
#{'email': <import_export.fields.Field: email>, u'id': <import_export.fields.Field: id>, 'title': <import_export.fields.Field: title>, 'stand_id': <import_export.fields.Field: stand_id>}
dataset = tablib.Dataset(['', '2', 'w@x.pl', 'fooo'],
headers=['id', 'stand_id', 'email', 'title'])
res = my_res.import_data(dataset, dry_run=True)
res.has_errors()
True
这是为什么呢?has_errors()
是在验证资源还是整个模型?
我的资源定义是:
a) 包含了模型 Exhibitor
的部分字段
b) 还添加了一个额外的字段 email
,这个字段在 Exhibitor
类中并不存在
更新:
我运行时设置了 raise_errors=True
res = my_res.import_data(dataset, dry_run=True,raise_errors=True)
它打印出:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/resources.py", line 350, in import_data
six.reraise(*sys.exc_info())
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/resources.py", line 335, in import_data
self.import_obj(instance, row, real_dry_run)
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/resources.py", line 201, in import_obj
self.import_field(field, obj, data)
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/resources.py", line 193, in import_field
field.save(obj, data)
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/fields.py", line 83, in save
setattr(obj, self.attribute, self.clean(data))
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/fields.py", line 51, in clean
value = self.widget.clean(value)
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/import_export/widgets.py", line 137, in clean
return self.model.objects.get(pk=pk) if pk else None
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/Users/andi/.virtualenvs/daftdjango/lib/python2.7/site-packages/django/db/models/query.py", line 307, in get
self.model._meta.object_name)
DoesNotExist: Stand matching query does not exist.
1 个回答
0
这个问题虽然很老了,但如果有人遇到同样的问题,这里有一个可能的解决办法。Django
报错的原因是因为模型 Exhibitor
有一个字段和 Stand
模型中的外键关联。其实我在自己的项目中也遇到过类似的问题,最后通过在模型中添加相应的值解决了。
在这个情况下,stand_id
应该在 Stand
模型中存在:
Stand 模型:
stand_id, my_column
2, my_value
然后我们可以使用:
dataset = tablib.Dataset(['', '2', 'w@x.pl', 'fooo'],
headers=['id', 'stand_id', 'email', 'title'])
因为 stand_id
的值 2
存在,所以错误应该就消失了……