[Answered ]-Wagtail: Dynamically load StreamField blocks in the admin EditView

1👍

This wouldn’t work, because the StreamField block definition isn’t just used for the edit form – any time you access that field, even just to output it on a template, the StreamField needs to look at the block definition to unpack the JSON and return the appropriate Python objects. For example, suppose the database contained:

[{'type': 'test', 'value': 123}]

What should page.attributes[0].value return? If the test block is of type IntegerBlock, it would return 123 – but if it was a PageChooserBlock instead, it would return the Page instance with ID 123. If it was an ImageChooserBlock, it would return the Image instance with ID 123, and so on. In your example, the only way to find out the block type is to call get_edit_handler and construct the entire editing interface as a side effect – this would be mixing up model-level logic with admin interface code, and be massively inefficient just for accessing a single field.

👤gasman

Leave a comment