wagtail / wagtail-factories

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

Make ListBlockFactory return a ListValue for Wagtail >= 2.16 #68

Closed jams2 closed 1 year ago

jams2 commented 1 year ago

Fixes #65

Wagtail 4.1 introduces the ReferenceIndex and functionality for tracking object usage in StreamFields. When Wagtail attempts to extract references from a StreamField that contains a ListBlock, it expects the value of the bound list block to be a ListValue.

This PR changes the ListBlockFactory so it returns a ListValue on Wagtail >= 2.16 (the version which introduced ListValue), and updates the tests to accomodate.

Note: there's still an issue with passing a list directly to a StreamField for a ListBlock value, e.g.:

MyTestPageFactory(
    parent=root_page, body=[("char_array", ["bla-1", "bla-2"])]
)

Wagtail won't currently "upcast" the list to a ListValue. As this is Wagtail behaviour it's not something we can/should fix here. If creating list values using the nested fields syntax, a ListValue will be created, e.g:

MyTestPageFactory(
    parent=root_page,
    body__0__char_array__0="bla-1",
    body__0__char_array__1="bla-2",
)

An alternative is to create a ListValue yourself:

MyTestPageFactory(
    parent=root_page, body=[("char_array", ListValue(ListBlock(CharBlock()), values=["bla-1", "bla-2"]))]
)