Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error creating object with ImageField #235

Closed
fcasco opened this issue Oct 8, 2015 · 2 comments
Closed

Error creating object with ImageField #235

fcasco opened this issue Oct 8, 2015 · 2 comments
Labels

Comments

@fcasco
Copy link

fcasco commented Oct 8, 2015

Given a factory with this code:

class SomeFactory(factory.DjangoModelFactory):

class Meta:
    model = SomeModel

image_field = factory.django.ImageField(filename='registration.jpg', width=1, height=1)

When I try to create the object using SomeFactory.create() I get this traceback:

Traceback (most recent call last:
  File "tests_frontend.py", line 351, in test_card_activation
    registration = SomeFactory.create()
  File "env/lib/python2.7/site-packages/factory/base.py", line 559, in create
    return cls._generate(True, attrs)
  File "env/lib/python2.7/site-packages/factory/base.py", line 484, in _generate
    obj = cls._prepare(create, **attrs)
  File "env/lib/python2.7/site-packages/factory/base.py", line 459, in _prepare
    return cls._create(model_class, *args, **kwargs)
  File "env/lib/python2.7/site-packages/factory/django.py", line 149, in _create
    return manager.create(*args, **kwargs)
  File "env/lib/python2.7/site-packages/django/db/models/manager.py", line 137, in create
    return self.get_query_set().create(**kwargs)
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 377, in create
    obj.save(force_insert=True, using=self.db)
  File "env/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "env/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "env/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 1593, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 911, in execute_sql
    for sql, params in self.as_sql():
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 872, in as_sql
    for obj in self.query.objs
  File "env/lib/python2.7/site-packages/django/db/models/fields/files.py", line 249, in pre_save
    file.save(file.name, file, save=False)
  File "env/lib/python2.7/site-packages/django/db/models/fields/files.py", line 90, in save
    self._size = content.size
  File "env/lib/python2.7/site-packages/django/db/models/fields/files.py", line 70, in _get_size
    return self.file.size
  File "env/lib/python2.7/site-packages/django/core/files/base.py", line 39, in _get_size
    elif os.path.exists(self.file.name):
AttributeError: 'cStringIO.StringI' object has no attribute 'name'

I was expecting the instance would be created with an image

@rbarrois rbarrois added the Bug label Oct 20, 2015
@rbarrois
Copy link
Member

Indeed, this is a known issue with factory.django.FileField and factory.django.ImageField that are implemented as post-build fields, thus not available in the field's save() method.

@fcasco
Copy link
Author

fcasco commented Apr 21, 2016

I solved using this code:

    image = factory.LazyAttribute(lambda o: ContentFile(
                                  factory.django.ImageField()._make_data(
                                    {'width': 8, 'height': 8, 'format': 'png'}), 'benefit.png'))

@fcasco fcasco closed this as completed Apr 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants