gpt4 book ai didi

python - 使用 Factory boy ImageFiled 导致 missing attribute _committed 错误

转载 作者:太空狗 更新时间:2023-10-30 01:37:50 26 4
gpt4 key购买 nike

我正在尝试为需要 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 导致 missing attribute _committed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com