from wagtail.admin.edit_handlers import StreamFieldPanel from wagtail.core import blocks from wagtail.core.fields import StreamField from wagtail.core.models import Page class MyPage(Page): body = StreamField([ ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('video', EmbedBlock()), ]) content_panels = Page.content_panels + [ StreamFieldPanel('body'), ]
from wagtail.admin.edit_handlers import BaseFieldPanel from wagtail.core.blocks.field_block import IntegerBlock class CustomStreamFieldPanel(BaseFieldPanel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.child_edit_handler_class = BaseFieldPanel def clone(self): return self.__class__( instance=self.instance, form=self.form, disabled=self.disabled, **self.kwargs ) def bind_to_model(self, model): self.model = model def on_model_bound(self): if not hasattr(self.model, self.field_name): self.model.add_to_class(self.field_name, StreamField([])) def widget_overrides(self): return {self.field_name: StreamFieldWidget(self)} def widget_media(self): return self.child_edit_handler_class.widget_media() + self.widget_overrides()[self.field_name].mediaThis example code creates a custom StreamFieldPanel class called 'CustomStreamFieldPanel' that inherits from the BaseFieldPanel class. The custom panel is designed to be used with a StreamField and overrides several protected methods to modify the panel's behavior. In this case, the panel's widget_overrides method specifies a StreamFieldWidget as the widget for the specified field_name. The custom panel can be used in place of the StreamFieldPanel in a Wagtail ModelAdmin content_panels list.