We have a setup with a Blog model that has a manytomany relation for BlogPageCategory, and we have a "recent blog posts" streamfield block that lets you specify whether to show cards for X latest blog posts, or X latest blog posts from a specific category.
As such, we started with the following code:
from wagtail.core import blocks
class RecentBlogEntries(blocks.StructBlock):
title = blocks.CharBlock(
required=True,
)
category_filter = blocks.ChoiceBlock(
label='Filter by Category',
required=False,
choices=[
('all', 'All'),
('First Category', 'First Category'),
('...',. '...'),
],
)
...
But hardcoding the categories is kind of silly, and being able to pick them from "what the list is, right now, based on the CMS data for BlogPageCategory" would be far more convenient. However, the following code (of course) turns into an equally hardcoded migration:
from wagtail.core import blocks
from ... import BlogPageCategory
class RecentBlogEntries(blocks.StructBlock):
title = blocks.CharBlock(
required=True,
)
choices = [ (cat.name, cat.name) for cat in BlogPageCategory.objects.all()]
choices.sort()
choices.insert(0, ('all', 'All'))
category_filter = blocks.ChoiceBlock(
label='Filter by Category',
required=False,
choices=choices,
)
...
Is there any way to make this a dynamic value instead of a list that is fixed by makemigrations
?
BlogPageCategory
object is created in the admin, migrations need to be run? – Potable