Is there a way to define a named parameter (that is not a model attribute) to control the behavior of factory.Maybe?
For example: I want to create a named parameter that controls the creation of a RelatedFactory through a Maybe condition, I tried to do this:
# factories.py
class Purchase(factory.DjangoModelFactory):
user = factory.SubFactory('project.factories.UserFactory')
total = uniform(10, 100)
class Meta:
model = Purchase
class UserFactory(factory.DjangoModelFactory):
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
has_purchase = False
purchases = factory.Maybe(
'has_purchase',
yes_declaration=factory.RelatedFactory(Purchase, 'purchase'),
no_declaration=None
)
class Meta:
exclude = ['has_purchase', 'purchases']
model = User
# tests.py
user_without_purchase = UserFactory.create()
user_with_purchase = UserFactory.create(has_purchase=True)
It seems that I can't change the value of has_purchase through the named parameter, it's always set as False. I don't know if it's because both fields are declared in exclude Meta attribute. And I have to put them in exclude or else DjangoModelFactory tries to use these fields when saving it to the database, which causes an Integrity error.
Is there a way to do that?