6

我正在尝试为需要django.db.models.ImageField. 我正在尝试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'

任何帮助表示赞赏,包括获取具有指定文件名的有效图像的其他方法的提示。提前致谢!

4

1 回答 1

1

您是否尝试过将factory.django.ImageField类用于模型上的图像字段?听起来它可能符合要求。

这是使用您的模型和test.jpg文件的文档中的修改示例:

class GalleryImageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.GalleryImage

    image = factory.django.ImageField(from_path='test.jpg')
于 2018-05-15T02:00:22.907 回答