我正在尝试为需要django.db.models.ImageField
的测试用例设置数据。我正在尝试使用Factory-boy的factory.django.ImageField
,但收到错误AttributeError: 'ImageField' object has no attribute '_committed'
简化的django对象:
class GalleryImage(models.Model):
image = models.ImageField(upload_to='uploads/products')
工厂类别:
class GalleryImageFactory(factory.DjangoModelFactory):
class Meta:
model = models.GalleryImage
测试中的用法:
img = factory.django.ImageField(filename='test.jpg')
GalleryImageFactory.create(image=img)
这将给我以下错误,指向创建
GalleryImageFactory
的行。AttributeError: 'ImageField' object has no attribute '_committed'
我正在运行python(2.7.6),factory-boy(2.4.1)和Django(1.6.8)
完整堆栈跟踪:
Traceback (most recent call last):
File "/home/vagrant/sportamore/tests/sportamor_tests/catalog/test_pricefeed_google.py", line 80, in setUp
image=img)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/factory/base.py", line 585, in create
return cls._generate(True, attrs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/factory/base.py", line 510, in _generate
obj = cls._prepare(create, **attrs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/factory/base.py", line 485, in _prepare
return cls._create(model_class, *args, **kwargs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/factory/django.py", line 153, in _create
return manager.create(*args, **kwargs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 157, in create
return self.get_queryset().create(**kwargs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 322, in create
obj.save(force_insert=True, using=self.db)
File "/home/vagrant/sportamore/sportamor/catalog/models/__init__.py", line 3960, in save
super(GalleryImage, self).save(*args, **kwargs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 902, in execute_sql
for sql, params in self.as_sql():
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 860, in as_sql
for obj in self.query.objs
File "/home/vagrant/sportamore/venv/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 250, in pre_save
if file and not file._committed:
AttributeError: 'ImageField' object has no attribute '_committed'
感谢所有帮助,包括获得具有指定文件名的有效图像的其他方法的技巧。提前致谢!
最佳答案
您是否尝试过将 factory.django.ImageField
类用于模型上的图像字段?听起来好像很合适。
这是使用模型和test.jpg
文件的文档修改后的示例:
class GalleryImageFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.GalleryImage
image = factory.django.ImageField(from_path='test.jpg')
关于python - 使用Factory boy ImageFiled会导致缺少属性_committed错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31809111/