class Organization(models.Model):
is_active = models.BooleanField()
name = models.SlugField(max_length=64, unique=True, blank=False, null=False)
description = models.TextField(blank=True, null=True)
class OrganizationFactory(factory.django.DjangoModelFactory):
class Meta:
model = Organization
is_active = factory.Faker("boolean", chance_of_getting_true=50)
name = factory.LazyAttributeSequence(lambda o, n: f"organization-{n}")
description = factory.Maybe(
factory.Faker("boolean", chance_of_getting_true=50),
yes_declaration=factory.Faker("text"),
no_declaration=None,
)
The issue
I want description to have a value only some of the time - regardless of the value of any other model fields. So although I could pass "is_active" as the first argument to factory.Maybe it doesn't quite fit my use-case.
I have managed to get it working using this ugly code:
from functools import partial
from faker import Faker
fake = Faker()
class OrganizationFactory(factory.django.DjangoModelFactory):
...
description = factory.Maybe(
factory.LazyFunction(partial(fake.boolean, chance_of_getting_true=50)),
yes_declaration=factory.Faker("text"),
no_declaration=None,
)
Description
Using a
factory.Faker
as the "decider" for afactory.Maybe
instead of the name of a model field causes the following error:To Reproduce
Model / Factory code
The issue
I want
description
to have a value only some of the time - regardless of the value of any other model fields. So although I could pass "is_active" as the first argument tofactory.Maybe
it doesn't quite fit my use-case.I have managed to get it working using this ugly code:
But that feels like a hack.
Notes
I feel like this was meant to have been fixed by https://github.com/FactoryBoy/factory_boy/pull/828.