Video

The Wagtail docs can't possibly cover everything, that's pretty unrealistic to expect perfect documentation. So in this video I will show you how to explore StreamField options that aren't always available in the documentation. You can apply what you'll learn in this video to any feature in Python, but we're using StreamFields as an example. Let's learn how to dive into a core feature of Wagtail!

Code & steps

Writing a StreamField without a custom class

For simple StreamFields you do not need to create custom classes. If you want to, that's 100% OK to do. Below you will see an example of a CharBlock that uses most of the available CharBlock kwargs.

from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.core import blocks as streamfield_blocks


class FlexPage(Page):
    """Flexible page class."""

    content = StreamField(
        [
            ("char_block", streamfield_blocks.CharBlock(
                required=True,
                help_text="Oh wow this is help text!!",
                min_length=10,
                max_length=50,
                template="streams/char_block.html",
            ))
        ],
        null=True,
        blank=True,
    )

If you're wondering how I know we can use these fields:

  • required
  • help_text
  • min_length
  • max_length
  • template

Well the TL;DR version is: I went to the source code https://github.com/wagtail/wagtail/blob/master/wagtail/core/blocks/field_block.py#L97

If you want to know how I found this, you'll need to watch the video and about half way through I start talking about how to find the source code by working backwards from my current code.

The Git Commit

Take a look at all the code I wrote in this tutorial: https://github.com/CodingForEverybody/learn-wagtail/commit/261adec1f07a4fe74f53610b69418b5f52484240

Sign up for our newsletter

Get notified about new lessons :)