[Django]-Factory-boy create a list of SubFactory for a Factory

34👍

Use factory.List:

class ParentOfUsers(factory.Factory):
    users = factory.List([
        factory.SubFactory(UserFactory) for _ in range(5)
    ])
👤Paul

5👍

You could provide a list with factory.Iterator

import itertools
import factory

# cycle through the same 5 users
users = itertools.cycle(
    (UserFactory() for _ in range(5))
)

class ParentFactory(factory.Factory):
    user = factory.Iterator(users)

Leave a comment