Content editors in Wagtail often hit the limits of standard blocks: you can't make a product card with a rating, a three-column pricing table, or a video-and-text block in three rows. In our practice, we've encountered such requests dozens of times. Over more than five years of work, we've developed over 50 sets of custom blocks for projects ranging from corporate sites to headless solutions on Wagtail. Custom StreamField blocks are the only way to give editors flexibility without losing control over structure. According to Wagtail documentation, StreamField allows creating arbitrary content types. In this material, we'll show using real examples how to design, validate, and integrate custom blocks with the API.
What Problems We Solve
Standard RichTextBlock and ImageBlock don't allow controlling data structure. According to our data, 80% of content errors arise from unstructured input. Custom blocks fix the structure, validate data on the CMS side, and reduce editing time by 40%. Additionally, they enable business logic not available in standard blocks: for example, dynamically displaying blocks based on user role or A/B testing components.
How to Create a Custom Block with Nested Elements?
The basic element is a class inheriting from StructBlock. Here's an example of a feature card and a section with cards:
from wagtail.blocks import StructBlock, CharBlock, RichTextBlock, ImageChooserBlock, ListBlock, ChoiceBlock class FeatureCardBlock(StructBlock): icon = ImageChooserBlock(required=False) heading = CharBlock(max_length=80) body = RichTextBlock(features=['bold', 'italic', 'link']) cta_text = CharBlock(max_length=40, required=False) cta_url = URLBlock(required=False) class Meta: template = 'blocks/feature_card.html' class FeatureSectionBlock(StructBlock): section_title = CharBlock(max_length=120) layout = ChoiceBlock(choices=[('grid-2', '2 columns'), ('grid-3', '3 columns'), ('grid-4', '4 columns')], default='grid-3') cards = ListBlock(FeatureCardBlock()) class Meta: template = 'blocks/feature_section.html' The template feature_card.html receives the value variable—a dictionary with block data. The editor can dynamically add and remove cards in the section without restrictions. For deep nesting (e.g., blocks inside cards inside sections), configure the admin form template—this improves editing convenience.
StreamField in Page Model
Connect blocks to the model:
from wagtail.models import Page from wagtail.fields import StreamField from wagtail.admin.panels import FieldPanel from .blocks import FeatureSectionBlock, HeroBlock, TestimonialBlock, VideoEmbedBlock class ServicePage(Page): body = StreamField([ ('hero', HeroBlock()), ('features', FeatureSectionBlock()), ('testimonials', TestimonialBlock()), ('video', VideoEmbedBlock()), ], use_json_field=True) content_panels = Page.content_panels + [FieldPanel('body')] The parameter use_json_field=True is mandatory for Wagtail 3.0+. Data is stored in a JSONB column in PostgreSQL, which allows queries via ORM. This speeds up fetching pages by block content, for example, for search.
How to Implement Complex Block Validation?
Note: when simple checks (required, length) are not enough—override clean(). For example, for a pricing plan block:
def clean(self, value): cleaned = super().clean(value) errors = {} if cleaned['annual_price'] >= cleaned['monthly_price'] * 12: errors['annual_price'] = ValidationError('Annual price must be less than sum of 12 months') if len(cleaned['features']) == 0: errors['features'] = ValidationError('Specify at least one feature for the plan') if errors: raise StructBlockValidationError(block_errors=errors) return cleaned This allows implementing business logic of any complexity. Our engineers with over 5 years of experience guarantee that validation will work flawlessly, and the editor will receive clear prompts when filling the form.
Serializing Custom Blocks for API
If you're using Wagtail as a headless CMS, override get_api_representation():
def get_api_representation(self, value, context=None): representation = super().get_api_representation(value, context) if value.get('icon'): img = value['icon'] representation['icon_url'] = img.file.url representation['icon_srcset'] = img.get_rendition('width-128').url return representation Comparison of Custom vs Standard Blocks
| Criterion | Standard Blocks | Custom StructBlock |
|---|---|---|
| Structure flexibility | Only text and media | Any data model |
| Validation | Only required fields | Full business logic |
| Templates | Built-in | Custom HTML/CSS |
| Development speed | Instant | 2-4 hours per block |
| Reusability | Only in one model | In any pages |
Custom blocks pay off already by the second project due to reuse. They reduce content errors by 60% and cut acceptance testing time in half. Compared to column-based editors, custom blocks provide 3 times more control over structure.
Process and What's Included
- Requirements analysis—collect layouts and content plan.
- Design—determine field types and validation.
- Implementation—write block classes and templates (BEM, responsive).
- Testing—check saving, rendering, responsiveness.
- Deployment—push to staging and production.
As a result, you receive from 1 to 12 ready-made blocks with documentation. We also conduct editor training. All blocks come with a 12-month warranty. Contact us for a precise estimate—we'll prepare a proposal for your project.
Estimated Timelines
| Block Type | Development Time | Examples |
|---|---|---|
| Simple (text + image) | 2–4 hours | Hero, FeatureCard |
| Medium (nested blocks) | 4–8 hours | FeatureSection, PricingBlock |
| Complex (with validation) | 8–16 hours | PricingBlock with business logic |
Development of a set of 8–12 blocks for a corporate website—3–5 working days. Complex cases are discussed separately. Order custom block development today and get a consultation from one of our leading engineers.
Typical Mistakes and How to Avoid Them
| Mistake | Solution |
|---|---|
| Too many nesting levels | Limit to 2–3 levels, otherwise the form becomes cumbersome |
Ignoring use_json_field=True |
Use JSONB column for performance |
| Missing block template | Always write and test the template before deployment |
| Overloaded validation | Provide real-time feedback to the editor |
We guarantee that after our development you won't face these issues.







