Skip to main content

BlockReference support for Wagtail - enables forward and cyclic block references

Project description

wagtail-block-reference

BlockReference support for Wagtail — enables forward and cyclic block references.

Heads up: this package is quite hacky. It works by monkey-patching Wagtail internals (Python and JS), which is inherently fragile. It exists because the upstream PR (wagtail#14279) may take a while to land (if it ever does), and waiting wasn't an option for me :). The test suite deliberately targets the patched internals so that a future Wagtail upgrade that breaks the patches fails loudly rather than silently misbehaving.

Installation

pip install wagtail-block-reference

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "wagtail_block_reference",
]

Usage

from wagtail import blocks
from wagtail_block_reference import BlockReference

class CommentBlock(blocks.StructBlock):
    text = blocks.CharBlock()
    replies = blocks.ListBlock(BlockReference(lambda: CommentBlock))

How it works

BlockReference is a lazy proxy that resolves its target block on first access. The target can be a lambda (for forward/cyclic references), a dotted import path, or a block class.

The package ships two patches:

Python patch (patches.py, applied at import time): hooks into Wagtail's block metaclass so that BlockReference attributes declared on a StructBlock are collected as child blocks, and registers a telepath adapter so the block serialises as its resolved target.

JS patch (patch.js, injected via insert_global_admin_js): intercepts window.telepath before Wagtail's own bundles load, then wraps StructBlock and StreamBlock prototypes with a lazy childBlockDefsByName getter. Without this, the cyclic telepath graph causes a Maximum call stack size exceeded crash when the editor tries to build the block name map at construction time.

Supported versions

All Wagtail versions that are currently under active or security support: 7.0 LTS, 7.3, and 7.4 LTS. CI tests against each of these. Versions drop off the matrix as they go end-of-life.

Development

# Install dependencies
uv sync

# Lint & format
uv run ruff check .
uv run ruff format .

# Run all tests (unit + E2E) against a single Wagtail version
uv run playwright install chromium
uv run pytest tests/

# Run all tests against all supported Wagtail versions (installs Chromium automatically)
uv run tox

# Run all tests against all supported Wagtail versions (installs Chromium automatically)
# and see visually what the browser is doing
uv run tox  -- --headed --slowmo=500

# Run against a specific version
uv run tox -e wagtail74

# Run against a specific version and see visually what the browser is doing
uv run tox -e wagtail74 -- --headed --slowmo=500

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_block_reference-0.2.0.tar.gz (61.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_block_reference-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file wagtail_block_reference-0.2.0.tar.gz.

File metadata

  • Download URL: wagtail_block_reference-0.2.0.tar.gz
  • Upload date:
  • Size: 61.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wagtail_block_reference-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dde1d7721d08a0369c270adf9f72f6e0b793ee98895d946330d837970c40c9f2
MD5 236f6a232889c83b43a526e2bbb49bb6
BLAKE2b-256 5f7ef0bd130b7596b7abf1cd7291d392d9ef0fb840b92743fba8ab7fa9891937

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_block_reference-0.2.0.tar.gz:

Publisher: publish.yml on joeyjurjens/wagtail-block-reference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wagtail_block_reference-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_block_reference-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6de8bf13b47b344733335805674238250806026d1a2ff30b9fc99dd4619d2dcc
MD5 235869d2f781cc6fa6b74acd2b1bb14a
BLAKE2b-256 5020b34512691f89592713890d483530d93aeb9501a4f814f01ce63c254bdd76

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_block_reference-0.2.0-py3-none-any.whl:

Publisher: publish.yml on joeyjurjens/wagtail-block-reference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page