Can StreamField listblock be optional (default to 0 items)

596 views
Skip to first unread message

Arthur Hanson

unread,
Oct 7, 2015, 4:51:06 AM10/7/15
to Wagtail support
I want a user to be able to add 0..X related links to a StreamField block.  Using a ListBlock (see below) this works, but an empty related-link field always appears in the admin when a user creates a new container-block - which is a problem as the user has to delete it if there are no related links or gets an error that it's fields are required when trying to save the model.  Is it possible to make this default to not appear, so the user has to click to add a related-link before the fields will appear?

I define a field to be a ListBlock of another StructBlock (links to related pages):

class LinkBlock(blocks.StructBlock):
    link_text = blocks.CharBlock(required=True)
    page = blocks.PageChooserBlock(required=True)

class PanelBlock(blocks.StructBlock):
    ...
    related_links = blocks.ListBlock(LinkBlock(required=False), required=False)

This works in that the user can click to add more related links, but when a panel block is added it will always show in the admin with an initial empty related-link block.  I want to make it so when a PanelBlock is created an empty panel-link is not shown in the admin, so the user needs to actually click to add one.  I've added the "required=False" to try and get this behavior to no effect.


Janne

unread,
Jan 11, 2019, 8:25:24 AM1/11/19
to Wagtail support
I just fought with the same problem. Looks like this works:

    related_links = blocks.ListBlock(LinkBlock(), default=[])

I.e. give the keyword 'default' with an empty list as its value.

I know, a bit too late to help you, but maybe someone else gets help from this. :)

Cheers,
Janne
Reply all
Reply to author
Forward
0 new messages