Support nested inline relations and typed tables in API v3 JSON writes
Problem statement
The v3 API accepts JSON, but its write path converts that JSON into data for Wagtail's existing Django forms and formsets. Reusing those forms makes sense for validation and revision handling. However, the conversion is currently incomplete for some ordinary Wagtail model structures, so applications need to patch internal API functions to make a JSON request round-trip.
For example, consider a page with an InlinePanel("sections"), where each section has its own InlinePanel("links"). A client should be able to create or edit it with JSON such as:
{
"sections": [
{
"title": "Introduction",
"links": [
{"label": "Documentation", "url": "https://example.com/docs"}
]
}
]
}wagtail.api.v3.form_data.build_form_data builds management-form data for the first-level sections relation, but does not descend into the section form's links formset. The JSON schema/form options already discover nested relations recursively, which makes this gap in the write conversion surprising.
A similar gap exists for TypedTableBlock values in a StreamField: their JSON columns, rows, and caption need to be expanded into the keys expected by the table widget. A JSON array of tag names also needs adaptation for TagField. These are API concerns that ideally should not require each Wagtail project to monkey-patch wagtail.api.v3.form_data.
Proposed solution
Extend the standard v3 JSON-to-form conversion so writable nested InlinePanel relations, typed tables, and tag arrays can be submitted in their normal API JSON representation. The implementation can continue to use Wagtail's existing forms for validation and saving; the client should not need to know their internal field names.
For nested formsets, recursively fill each child form's declared formsets, including management-form counts and existing child IDs. Preserve PATCH semantics: an omitted relation remains unchanged, while a supplied list replaces that relation's contents. Existing IDs should update the corresponding children; omitted IDs should create new children; an explicit empty list should clear the relation.
For typed tables, flatten each cell according to its declared column block. Tag names supplied as JSON strings should retain punctuation, including commas within a single tag.
Alternative solutions
A local workaround is to wrap Wagtail's converter and recurse into nested child formsets. This excerpt from that approach shows the recursion:
def _fill_child_formsets(data, formset_class, item, prefix, instance):
for relation, nested_class in getattr(formset_class.form, "formsets", {}).items():
_fill_formset(
data, nested_class, item.get(relation),
f"{prefix}-{relation}", instance, relation,
)The omitted _fill_formset helper builds management-form keys, matches existing IDs, marks deleted rows, and calls _fill_child_formsets for each row. Typed-table support similarly extends flatten_block_value to emit column, row, and cell keys. Project-level monkey-patching of internal functions is fragile across Wagtail upgrades, so this belongs in the v3 API itself or in a documented extension point.
Additional context
This is about the v3 write API, not the older read-only v2 API. Related StreamField write work was tracked in #14310. A useful regression test would POST/PATCH the JSON above, read the draft back, and assert that both levels of children survive; analogous round-trip tests could cover typed tables and tag names containing commas.
Working on this
Contribute to the discussion to help this move forward. Explain your use cases, discuss possible solutions, research how to adapt existing code, or compatibility concerns.
Once others agree with the proposed changes, anyone can contribute to this. View our contributing guidelines. Add a comment to the issue to explain what approach you are taking.
Source: wagtail/wagtail