Utilities for traversing Wagtail StreamField block definitions and values
Project description
wagtail-traverse
Utilities for traversing Wagtail StreamField and StreamValue objects.
Installation
pip install wagtail-traverse
Usage
This is a set of two functions:
-
traverse_block(block, parent=None)walks a StreamField's block definition tree (the schema, not page data).Given a root block, it yields back
(path, block)tuples for every block reachable from that root, including the root itself. This is intended to be used to discover what block types and paths can exist in a StreamField.from wagtailtraverse import traverse_block streamfield = MyPage._meta.get_field("body") for child_name in streamfield.stream_block.child_blocks: for path, block in traverse_block( streamfield.stream_block.child_blocks[child_name] ): print(f"{path}: {block.__class__.__name__}")
For a page with a
bodyfield containingheading,paragraph, and aheroStructBlock that has animageandcaption, this would print:heading: CharBlock paragraph: RichTextBlock hero: StructBlock hero.image: ImageChooserBlock hero.caption: CharBlock -
traverse_value(value, parent=None)walks the value of a StreamField on a page.It yields
(path, bound_block)tuples for every block in the stream value, recursing intoStreamValues,StructValues,ListValues, andTypedTables. This is intended to be used for deeply inspecting or migrating the content stored in a StreamField.from wagtail_traverse import traverse_value page = MyPage.objects.get(slug="example") for path, bound_block in traverse_value(page.body): print(f"{path}: {bound_block.block.__class__.__name__} = {bound_block.value}")
Paths
Both functions produce dotted path strings like hero.image or section.list.item that describe where a block sits in the tree.
License
This project is licensed under the MIT License. See LICENSE for details.
These functions originate in the wagtail-content-audit library as part of a block usage auditing. I have broken them out in this small utility library for convenience, since the ability to traverse StreamFields and their values is something I occasionally have found need to do.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wagtail_traverse-1.0.1.tar.gz.
File metadata
- Download URL: wagtail_traverse-1.0.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f39014fe6cf94f9981f3841563d325ed2552bdd0efea50c9842d2f91a8964f14
|
|
| MD5 |
9b2b3aea2ff66aad471d3ff896d3415d
|
|
| BLAKE2b-256 |
eb29696ccb3e8e7c778b68a12e5cd99223f080b6b666954f62ae35cebc5936ea
|
Provenance
The following attestation bundles were made for wagtail_traverse-1.0.1.tar.gz:
Publisher:
publish.yml on willbarton/wagtail-traverse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_traverse-1.0.1.tar.gz -
Subject digest:
f39014fe6cf94f9981f3841563d325ed2552bdd0efea50c9842d2f91a8964f14 - Sigstore transparency entry: 1222496743
- Sigstore integration time:
-
Permalink:
willbarton/wagtail-traverse@8dda74355bd4aae1635624479d14a756067b3826 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/willbarton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8dda74355bd4aae1635624479d14a756067b3826 -
Trigger Event:
release
-
Statement type:
File details
Details for the file wagtail_traverse-1.0.1-py3-none-any.whl.
File metadata
- Download URL: wagtail_traverse-1.0.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4f4d1cad2db48744742a07479f283f88536aac35cbfe68f15b3c13bf4581fc9
|
|
| MD5 |
590ffc3c11ca058d28833dd79632e765
|
|
| BLAKE2b-256 |
4fd77c8c0ed062657803328cc9825d7ed4b7dadc2042a0f26d21a1399a1a3861
|
Provenance
The following attestation bundles were made for wagtail_traverse-1.0.1-py3-none-any.whl:
Publisher:
publish.yml on willbarton/wagtail-traverse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_traverse-1.0.1-py3-none-any.whl -
Subject digest:
d4f4d1cad2db48744742a07479f283f88536aac35cbfe68f15b3c13bf4581fc9 - Sigstore transparency entry: 1222496824
- Sigstore integration time:
-
Permalink:
willbarton/wagtail-traverse@8dda74355bd4aae1635624479d14a756067b3826 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/willbarton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8dda74355bd4aae1635624479d14a756067b3826 -
Trigger Event:
release
-
Statement type: