factory_boy
factory_boy copied to clipboard
Many To Many field fixture special function
The problem
Its a bit annoying to write list init for many to many fields in the following way:
(Example)
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
email = factory.Faker("email")
@factory.post_generation
def tickets(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for access in extracted:
self.tickets.add(access)
@factory.post_generation
def accesses(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for access in extracted:
self.accesses.add(access)
to be used with user = UserFactory(tickets=[TicketFactory()], accesses= [AccessFactory(),...]
Proposed solution
I propose to create special wrapper which will work in the following way:
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
email = factory.Faker("email")
tickets = factory.django.ManyToManyFromList() # function like above will be returned
accesses = factory.django.ManyToManyFromList()
Extra notes
Btw, I can create a PR
What is the interest of adding the list of related object as a kwarg to the user factory versus the following?
>>> user = UserFactory.create()
>>> user.tickets.add([TicketFactory()]])
we will be able to handle the creation of the object and creation of the nested many to many fields in one line, so if we create many such objects manually we will have less overloaded code (one line for each object creation instead of two)