Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Factory boy ImageFiled results in missing attribute _committed error

I'm trying to set up data for a test case that requires an django.db.models.ImageField. I'm trying to use factory.django.ImageField from factory-boy, but get the error AttributeError: 'ImageField' object has no attribute '_committed'

Simplified django object:

class GalleryImage(models.Model):
    image = models.ImageField(upload_to='uploads/products')

Factory class:

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

usage in test:

img = factory.django.ImageField(filename='test.jpg')
GalleryImageFactory.create(image=img)

This will give me the following error pointing to the line creating the GalleryImageFactory.

AttributeError: 'ImageField' object has no attribute '_committed'

I'm running python (2.7.6), factory-boy (2.4.1) and Django (1.6.8)

Full stack trace:

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'

Any help appreciated, including tips of other methods of getting a valid image with the specified filename in place. Thanks in advance!

like image 603
edruid Avatar asked Aug 04 '15 12:08

edruid


1 Answers

Have you tried using the factory.django.ImageField class for the image field on your model? It sounds like it might fit the bill.

Here's a modified example from the docs using your model and the test.jpg file:

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

    image = factory.django.ImageField(from_path='test.jpg')
like image 132
Franey Avatar answered Oct 08 '22 14:10

Franey