Ashish Pitrodafield notes
All episodes

Wagtail Unboxed episode 05 7:56

StreamField, properly

We replace the rich-text blob with real content blocks — and hit the migration error that stops most people on their first try.

What this episode covers

If you have ever seen CHECK constraint failed: JSON_VALID and had no idea why, that is in here, with the fix and the reason: why the migration fails on any page that already has content, and how to write a data migration that runs before the column changes type.

The blocks themselves: what StreamField actually is, a JSON column holding an ordered list of typed blocks; the four kinds of block and when to reach for each; StructBlock against ListBlock, which is record against repeat and the one people mix up; and Meta — icon, label and template, and what the editor sees.

Then the template side: the one-line change on the page template, how tiny a block template really is, and block_counts on a StreamBlock against max_num on a ListBlock.

Two gotchas, both hit while building the episode: the migration failure, and the reason a call to action rendered as a teal box inside a teal box. Also: stop passing use_json_field=True — Wagtail 7.4 accepts it and ignores it.

Chapters

  1. 0:00 One field, one blob
  2. 0:20 What StreamField actually is
  3. 0:38 The four kinds of block
  4. 1:01 StructBlock vs ListBlock
  5. 1:21 Writing a StructBlock
  6. 1:40 A struct containing a list
  7. 1:57 The top-level StreamBlock
  8. 2:14 The model change
  9. 2:27 use_json_field is dead
  10. 2:48 The migration blows up
  11. 3:01 What that error actually means
  12. 3:24 Nothing is broken
  13. 3:47 The fix: convert the data first
  14. 4:07 Writing the data migration
  15. 4:33 It applies
  16. 4:42 That unreadable block_lookup
  17. 5:04 One line in the page template
  18. 5:17 Inside a block template
  19. 5:33 A teal box inside a teal box
  20. 5:47 Wagtail already wrapped your block
  21. 6:06 Style the wrapper instead
  22. 6:19 What the client sees
  23. 6:41 Two ways to say “max”
  24. 7:01 The finished home page
  25. 7:17 The migrated About page
  26. 7:31 Commit and tag
  27. 7:34 Next episode

Versions on screen

Wagtail 7.4.3 · Django 6.1.1 · Python 3.12. Recorded on Windows; macOS and Linux commands are in the repository for every episode.

Next: episode 06, Images and documents

Carry on from here.

Episode 06 or see all episodes