wagtail / wagtail-factories

Factory boy classes for wagtail
http://wagtail-factories.readthedocs.io/en/latest/
MIT License
102 stars 41 forks source link

Feature Request: Add support for specifying a size to ListBlockFactory (like RelatedFactoryList) #67

Open johncarter-phntm opened 2 years ago

johncarter-phntm commented 2 years ago

It'd be really handy to have ListBlockFactory generate a list of N blocks, like RelatedFactoryList does, I think that's currently not possible unless we pass parameters to those sub-blocks.

jams2 commented 2 years ago

Hi @johncarter-phntm , could you give an example of how you would use this?

Potentially related: https://github.com/wagtail/wagtail-factories/issues/57

johncarter-phntm commented 2 years ago

Hi, my use case is I want some blocks to be generated by the subfactory by default.

eg:

class MyBlockFactory(wagtail_factories.StructBlockFactory):
    items = wagtail_factories.ListBlockFactory(MyBlockItemFactory, size=3)

    class Meta:
        model = models.MyBlock

Also having support for lambda like RelatedFactoryList might be nice:

class MyBlockFactory(wagtail_factories.StructBlockFactory):
    items = wagtail_factories.ListBlockFactory(MyBlockItemFactory, size=lambda: random.randint(1, 5))

    class Meta:
        model = models.MyBlock

I'm thinking if individual elements were specified with items__0 etc this would still fill in up to size elements.