Let's say I have:
class CompanyFactory(DjangoModelFactory):
class Meta:
model = Company
name = factory.Faker("company")
address = factory.Faker("address")
class InvoiceFactory(DjangoModelFactory):
class Meta:
model = Invoice
company = factory.SubFactory(CompanyFactory)
num = factory.Faker("numerify", text="#"*10)
value_total = factory.Faker("random_number", digits=3)
When I run the InvoiceFactory.create() method a new Invoice entry is created. And because Invoice "links" (foreign key) to Company, a new Company entry is also created.
What would be the right way of rewriting this so that when calling InvoiceFactory.create() an existing company entry is picked, instead of creating a new one?
You have two options:
class InvoiceFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Invoice
company = factory.Iterator(models.Company.objects.all())
The iterator is evaluated lazily, when the first invoice is created. However, it will always cycle through the same factories.
First, add a django_get_or_create attribute to your CompanyFactory: if the provided name already exists in the database, the existing instance will be reused:
class CompanyFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Company
django_get_or_create = ['name']
name = factory.Faker('company')
address = factory.Faker('address')
Then, provide values for the name field from a constant list (here using factory.fuzzy.FuzzyChoice):
class InvoiceFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Invoice
company = factory.SubFactory(
CompanyFactory,
name=factory.fuzzy.FuzzyChoice(['PSF', 'Django', 'The Spanish Inquisition']),
)
Once the 3 first companies have been created, they will be reused for all following objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With