I have a Wagtail project with two different Page
models that use identical gallery blocks, but in different ways. In one model, I want to use the gallery in a list of other freeform blocks; but in the other model there is only one gallery appearing in a specific place on the page. The galleries are pretty complex, with lots of options like autoadvance
that I don’t want to repeat between models, and which aren’t easy to move into a mixin. I can accomplish what I want by adding an extra gallery
StreamField to the strict model, like this: (content_panel
etc. omitted)
# models.py
from wagtail.models import Page
from wagtail.core.fields import RichTextField, StreamField
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core.blocks import (
RichTextBlock,
StructBlock,
StreamBlock,
)
class ImageWithCaptionBlock(StructBlock):
image = ImageChooserBlock()
caption = RichTextBlock()
class GalleryBlock(StructBlock):
# lots of gallery options like “autoadvance”
items = StreamBlock([
('image', ImageWithCaptionBlock()),
('video', EmbedBlock()),
])
class FreeFormContentPage(Page):
body = StreamField([
('richtext', RichTextBlock()),
('gallery', GalleryBlock()),
], use_json_field=True)
class StrictContentPage(Page):
body = RichTextField()
gallery = StreamField([
('gallery', GalleryBlock()),
], use_json_field=True, max_num=1)
This works but it looks a little weird when editing a StrictContentPage
!
What I’m looking to do is something like this:
class StrictContentPage(Page):
body = RichTextField()
gallery = GalleryBlock()
(or the equivalent via tweaking the admin)
… so that the admin experience looks like:
Thanks in advance for any pointers!