factory-boy: Passing "size" on the fly to RelatedFactoryList

Viewed 178

Given the following factories, one using RelatedFactoryList to create 2 Prices related to the Product:

class PriceFactory(factory.django.DjangoModelFactory):
    name = factory.Faker("sentence", nb_words=2)

    class Meta:
        model = models.Price

class Product(factory.django.DjangoModelFactory):
    name = factory.Faker("sentence", nb_words=2)
    prices = factory.RelatedFactoryList(
        PriceFactory,
        factory_related_name="product",
        size=2,
    )

    class Meta:
        model = models.Product


       

I know that I can pass information to the related factory doing something like this:

 product = ProductFactory.create(prices__name="All have this name")

Is there a way I can also change the size, or count, of prices created using an argument to ProductFactory.create? This does not work:

 product = ProductFactory.create(prices__size=5)
0 Answers
Related