paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
391 stars 67 forks source link

Problem with django-filer #65

Closed Matt-Deacalion closed 4 years ago

Matt-Deacalion commented 9 years ago

I'm running Django 1.8.2 with django-filer 0.9.10 and django-dynamic-fixture 1.8.4. I have an example model like this:

from django.db import models
from filer.fields.image import FilerImageField

class Animal(models.Model):
    name = models.CharField(max_length=100)
    picture = FilerImageField(null=True, blank=True)

Using the get function in my unit tests, like so:

G(Animal)

I get this traceback:

Creating test database for alias 'default'...
E
======================================================================
ERROR: test_animal (animals.tests.AnimalTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/matt/projects/ddf-bug/animals/tests.py", line 8, in test_animal
    instance = G(Animal)
  File "/home/matt/...trimed.../django_dynamic_fixture/__init__.py", line 106, in get
    return d.get(model, shelve=shelve, **kwargs)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 528, in get
    instance = self.new(model_class, shelve=shelve, named_shelve=named_shelve, **kwargs)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 455, in new
    self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 366, in set_data_for_a_field
    data = self._process_field_with_default_fixture(__field, model_class, persist_dependencies)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 351, in _process_field_with_default_fixture
    data = self._process_foreign_key(model_class, field, persist_dependencies)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 334, in _process_foreign_key
    data = fixture.get(next_model)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 528, in get
    instance = self.new(model_class, shelve=shelve, named_shelve=named_shelve, **kwargs)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 455, in new
    self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
  File "/home/matt/...trimed.../django_dynamic_fixture/ddf.py", line 383, in set_data_for_a_field
    setattr(__instance, __field.name, data) # Model.field = data
AttributeError: can't set attribute

----------------------------------------------------------------------
Ran 1 test in 0.014s

FAILED (errors=1)
Destroying test database for alias 'default'...

As a short term fix until I get a chance to look into this more I've added all of the FilerImageField fields I have to DDF_IGNORE_FIELDS in my Django settings, i.e.

DDF_IGNORE_FIELDS = ['picture',]
paulocheque commented 9 years ago

Thanks for reporting. I think I understand the problem.

FilerImageField is actually a ForeignKey (it inherits from ForeignKey). So DDF does not understand that and threats the field as a normal field.

So, DDF need to add support for "Custom ForeignKey fields".

I think you can try adding a custom data fixture for the field using DDF_FIELD_FIXTURES.

Or you can use DDF_FILL_NULLABLE_FIELDS = False

Or you can use DDF_USE_LIBRARY too.

Check this:

http://ddf.codeart.io/en/latest/ddf.html#global-settings

Matt-Deacalion commented 9 years ago

Thank you! Using custom fixtures or setting DDF_FILL_NULLABLE_FIELDS to False work nicely.

I haven't tried the Library / Shelve feature yet, as I'm not sure what it is exactly - I'll have a read. :-)