wagtailwagtail-snippet

Wagtail Admin: How to Add ReadOnlyBlocks to show snippets fields values in a StructBlock


I have a Snippet model(SampleSnippet) with fields field_a, field_b, and field_c. And I am using it in a StructBlock like:

class SampleBlock(blocks.StructBlock):
    service_snippet = SnippetChooserBlock(SampleSnippet)
    ...

Now I want to show some of the SampleSnippet fields(field_a and field_b) in SampleBlock after saving the SampleBlock for the first time(after saving the snippet) on the wagtail admin site. How can I achieve this? I looked around a lot but couldn't find a similar issue.

If it were a model/snippet, I could have used property with ReadOnlyPanel but in the case of a StructBlock, I am stuck and not getting any ideas. If anybody knows any way to achieve this, please help me out. Thanks in advance.


Solution

  • This is going to be quite difficult, since in current Wagtail versions (>=2.13) the StreamField editing UI is populated client-side in Javascript - as such, there are some extra steps to make the necessary data available to the client-side code, and the standard Django form / template mechanisms won't be available.

    A simple workaround, if the fields aren't too long, would be to define the snippet's __str__ method to include the desired data - this will then display any time the snippet is shown in the admin.

    For a full solution, I'd suggest this approach: