Field for single (Struct)block

547 views
Skip to first unread message

Roel Obdam

unread,
Dec 22, 2016, 11:09:35 AM12/22/16
to Wagtail support
The code below works, but I would like to have a two panels in the wagtail admin on this page for the two block (left and right).
In the docs it says you can only feed StreamBlock to StreamField, but is there a way to to have a field for a single block instance? Or should I use separate fields on the page instead of blocks?
When I try the generic FieldPanel as a panel i get 'StreamValue' object has no attribute 'get' (struct_block.py:line 78)


from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page
from wagtail.wagtailimages.blocks import ImageChooserBlock

class Block(blocks.StructBlock):
heading = blocks.CharBlock(required=True)
content = blocks.RichTextBlock()


class SimplePage(Page):
left = StreamField(Block)
    right = StreamField(Block)


Cheers,
Roel

Matthew Westcott

unread,
Dec 22, 2016, 11:52:48 AM12/22/16
to wag...@googlegroups.com
Hi Roel,
No, this isn't supported - the top-level block of a StreamField must be a StreamBlock, and there is no way to use blocks outside of StreamField.

In this situation, your best option is to define the page content as separate fields:

class SimplePage(Page):
left_heading = fields.CharField(max_length=255)
left_content = RichTextField()
right_heading = fields.CharField(max_length=255)
right_content = RichTextField()

Cheers,
- Matt

Roel Obdam

unread,
Dec 22, 2016, 12:25:57 PM12/22/16
to Wagtail support
Thanks for the answer Matthew.

Do you think that a field that can hold a single block would be a common use-case? The solution you propose does not use the nice reusable Block structures.
Or the stream field could have a cardinality option...

Thanks again for all your work!

Op donderdag 22 december 2016 17:52:48 UTC+1 schreef Matthew Westcott:

Matthew Westcott

unread,
Dec 22, 2016, 1:20:46 PM12/22/16
to wag...@googlegroups.com
Yes, I think it would be a worthwhile addition to have alternatives to StreamField that accept different block types at the top level. There's a ticket for that here: https://github.com/wagtail/wagtail/issues/2048

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/4dac447d-1663-4769-9081-56d253f1ed61%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages