wagtail: Create fields for structural block types
I want to make a field that’s a simple list downloads. The following code accomplishes what I want using StreamField, but the API is a little weird.
class ResourcesPage(Page):
files = StreamField([
('files', DocumentChooserBlock())
])
Ideally it would look something like this:
class ResourcesPage(Page):
files = ListField(DocumentChooserBlock())
Here I’ve created a new imaginary type, ListField. There are a few possible solutions (such changing the way StreamField parameters work) but I think it makes sense to have a wrapper field around each of the structural block types.
Here’s an example of how an implementation could look:
from wagtail.wagtailcore.fields import StructField, ListField, StreamField
from wagtail.wagtaildocs.blocks import DocumentChooserBlock
from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailcore import blocks
class RecipePage(Page):
# StructBlock wrapper
chef = StructField([
('first_name', blocks.CharBlock(required=True)),
('surname', blocks.CharBlock(required=True)),
('photo', ImageChooserBlock()),
('biography', blocks.RichTextBlock())
])
# ListBlock wrapper
ingredients_list = ListField(blocks.CharBlock(label="Ingredient"))
# StreamBlock (just a regular StreamField)
instructions = StreamField([
('paragraph', blocks.RichTextBlock(icon="pilcrow")),
('h1', blocks.CharBlock(classname="title", icon="title"))
])
This could also help us determine how the fields should appear in the admin (example: #1549)
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 20
- Comments: 20 (4 by maintainers)
The core team has discussed this today, and we’re happy with the principle of adding
ListField/StructFieldtypes in parallel with StreamField (or, perhaps more simply, allowing StreamField to acceptListBlock/StructBlockas the top-level block).In my opinion, the best approach is to have an specific field for each structural block type:
StructFieldforStructBlockListFieldforListBlockStreamFieldforStreamBlockRegarding to the wagtail admin views:
StructFieldwouldn’t need to include a “new” button, just render the block form.ListFieldwouldn’t need to show the stream menu to choose the model to add.StreamFieldwould work as usualOf course
ListFieldcould be considered just a particular case ofStreamFieldand be omitted, but ifListBlockexists, I thinkListFieldshould exists too.Any updates on this?
I agree with this:
Any updates?
Any updates on this ?
I just asked about this on Slack and was pointed here. This would be a nice to have feature that would make the models a little cleaner. My use case is that I have a collection of pre-defined block types that go into a technical content page. I want to be able to add blocks as fixed blocks (hence the need for a StructBlock), or stream fields, which already works beautifully.
The options available are:
A “StructField” could take a block, similar to a StreamField, but accept just a single block, hence it would work just like a stream field from a DB perspective, but have a single block item, rather than an array of items as a StreamField does.
Just thinking out loud
Bringing this back up, because I do need a ListField for the project I’m working on.
In this case, it makes sense to have something like:
I was hoping this feature was out already while scrolling down the page. 😦
+1
This would be so very very useful. I’m willing to bet this would get a lot of use of it existed.
I already have a streamfield block which already does what I want, but I need a given set of pages to have a single optional instance of said block separate from the main page stream field. Trying to achieve the same thing using Django models and an inline field just isn’t possible, or is very painful.
A BlockField() would solve my problem for me. The GUI would make sense, and it would allow reusing the existing block template.
If someone points me where to start I’d gladly help with this, or am more than happy to help review or debug any work towards this.
I would use this feature for sure. I have in multiple places StreamField with a single Block in it and limiting the StreamField to one block. But the UI is not great as you can add another block and when you save it says you can only have one.
2 options:
Following - this kind of comes down to defining reusable fieldsets more than anything else and it’s an issue I face frequently in wagtail dev!
For a site we are developing now, I created a “workaround”
StructFieldclass that can be used to addStructBlocks directly to your page. It feels a bit hacky, but makes my page models so much cleaner. Note: not tested in production, and we have a headless setup so I’m only interested in the API output (I did not check how this behaves in a template).This would be an awesome feature to have for the upcoming LTS 😃
I’ll work on that.