FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.53k stars 399 forks source link

FactoryBoy fails when custom Manager is defined on abstract model (multi inheritance) #231

Open user0007 opened 9 years ago

user0007 commented 9 years ago

The fix from #201 does not include multi inheritance. Eg:

class CustomManager(models.Manager):
    pass

class BaseModel(models.Model):
    custom_objects = CustomManager()

    class Meta:
        abstract = True

class BaseAPIModel(BaseModel):
    username = models.EmailField()

    class Meta:
        abstract = True

class MyModel(BaseAPIModel):
    pass

class MyModelFactory(factory.DjangoModelFactory):
    class Meta:
        model = MyModel

MyModelFactory.create()

Traceback (most recent call last):
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/factory/base.py", line 559, in create
    return cls._generate(True, attrs)
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/factory/base.py", line 484, in _generate
    obj = cls._prepare(create, **attrs)
  File "/home/myuser/projects/myproject/myproject/apps/users/tests/factories.py", line 11, in _prepare
    user = super(BaseUserFactory, cls)._prepare(create, **kwargs)
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/factory/base.py", line 459, in _prepare
    return cls._create(model_class, *args, **kwargs)
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/factory/django.py", line 144, in _create
    manager = cls._get_manager(model_class)
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/factory/django.py", line 118, in _get_manager
    manager = model_class.objects
  File "/home/myuser/projects/venvs/myproject/local/lib/python2.7/site-packages/django/db/models/manager.py", line 267, in __get__
    self.model._meta.object_name,
AttributeError: Manager isn't available; BaseAPIModel is abstract

Everything works ok when using pure models:

>>> MyModel.objects.all()
[...]
>>> MyModel.custom_objects.all()
[...]
frague59 commented 9 years ago

+1

blairg23 commented 5 years ago

No PRs yet on this one? 4 years after first mention?

rbarrois commented 5 years ago

@blairg23 Can you confirm that the issue still exists? In that case, it would be nice to have a minimal code sample that reproduces it, so that we can be confident in the quality of our fix.