3

I have a page with one StreamField body and a custom block named CardBlock:

class CardBlock(blocks.StructBlock):
    title = blocks.CharBlock()
    description = blocks.CharBlock()
    ...

class CardListBlock(blocks.StructBlock):
    cards = ListBlock(CardBlock())

class CustomPage(Page):
    ...
    body = StreamField([
        ('card_block', CardBlock()),
        ('card_list', CardListBlock())
    ]),
    ...

I need to create a CustomPage programatically with card_list populated.

I tried the solution provided by @gasman. While it works for a normal StructBlock, when having nested ListBlock within a StructBlock, the value is not saved.

I tried the following:

page = CustomPage()
page.body = [
    ('card_block', {'title': 'foo', 'description': 'bar'}),
    ('card_list', {
        'cards': [{'title': 'foo', 'description': 'bar'}]
    })
]

results in AttributeError: 'list' object has no attribute 'bound_blocks'

So, I wrapped the list in ListValue from wagtail.blocks.list_block.

page.body = [
    ('card_block', {'title': 'foo', 'description': 'bar'}),
    ('card_list', {
        'cards': ListValue([{'title': 'foo', 'description': 'bar'}]))
    }
]

It would result in no errors, but when looked into db, the card_list would have an empty list.

I tried using StructValue:

('card_list', {
    'cards': ListValue([
        StructValue([('title', 'foo'), ('description', "bar")])
    ])
})

same result.

So, I tried to use the data result by saving the Page from wagtail admin with no avail.

('card_list', {
    'cards': ListValue([{
         "type": "item",
         "value": {
             "title": "foo", 
             "description": "bar"},
         "id": "uuid"
    }])
})

Is there something I am missing out on this?

0 Answers0