Skip to content

Create fields for structural block types #2048

@alexgleason

Description

@alexgleason

I want to make a field that's a simple list downloads. The following code accomplishes what I want using StreamField, but the API is a little weird.

class ResourcesPage(Page):
    files = StreamField([
        ('files', DocumentChooserBlock())
    ])

Ideally it would look something like this:

class ResourcesPage(Page):
    files = ListField(DocumentChooserBlock())

Here I've created a new imaginary type, ListField. There are a few possible solutions (such changing the way StreamField parameters work) but I think it makes sense to have a wrapper field around each of the structural block types.

Here's an example of how an implementation could look:

from wagtail.wagtailcore.fields import StructField, ListField, StreamField
from wagtail.wagtaildocs.blocks import DocumentChooserBlock
from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailcore import blocks


class RecipePage(Page):
    # StructBlock wrapper
    chef = StructField([
        ('first_name', blocks.CharBlock(required=True)),
        ('surname', blocks.CharBlock(required=True)),
        ('photo', ImageChooserBlock()),
        ('biography', blocks.RichTextBlock())
    ])

    # ListBlock wrapper
    ingredients_list = ListField(blocks.CharBlock(label="Ingredient"))

    # StreamBlock (just a regular StreamField)
    instructions = StreamField([
        ('paragraph', blocks.RichTextBlock(icon="pilcrow")),
        ('h1', blocks.CharBlock(classname="title", icon="title"))
    ])

This could also help us determine how the fields should appear in the admin (example: #1549)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions