carlosayam
carlosayam

Reputation: 1398

where is FACTORY_FOR in a factory boy django factory?

I am using Factory Boy in a Django project and I need to access class properties, defined in the class, in the corresponding factory.

My test case has something along the lines of:

from django.test.testcases import TransactionTestCase
from app.tests.factories import SomeModelFactory

class MyTestCase(TransactionTestCase):   
    def test_something(self):
        ...
        SomeModelFactory.create(
            data_type=SomeModelFactory.FACTORY_FOR.TYPE_STRING)
        ...

My class, as you may guess, has the corresponding constant in the right spot.

class SomeModel(models.Model):
  TYPE_STRING = 2
  ...

and the corresponding factory is defined as it should be

class SomeModelFactory(factory.Factory):
  FACTORY_FOR = SomeModel
  ...

The test case fails saying that FACTORY_FOR is not there. So, I think the metaclass is hidding that somewhere. Ideally, I would like to minimise the need to import the model directly if I am using the factory.

Upvotes: 1

Views: 685

Answers (1)

Bernhard Vallant
Bernhard Vallant

Reputation: 50796

SomeModelFactory._associated_class should be what you are looking for...

Upvotes: 1

Related Questions