StreamField should consistently use bulk_to_python() to improve efficiency
See original GitHub issueIs your proposal related to a problem?
It’s very common in projects that I work on to define blocks that have a heading of some kind, followed by a curated list of pages, snippets or other objects (maybe up to 10 or so). Currently, when rendering a page with such a block in place, as many queries as there are items are made to fetch the necessary data, which clearly isn’t as efficient as it could be.
Describe the solution you’d like
Nested instances of ListBlock should utilise bulk_to_python()
to fetch multiple objects at once.
An added bonus would be adding some simple way of optimising further, by allowing some control over how ‘prefetch_related’ and ‘select_related’ are used in bulk_to_python()
. Perhaps they could be supplied as arguments, or each block could look for streamvalue_select_related
and streamvalue_prefetch_related
attributes on the target model, or a for_streamvalue()
method on the queryset itself?
Additional context
It looks as though StreamValue
should already support this, but it seems not to be working past the first level for me when testing locally. The built-in SnippetChooserBlock
, PageChooserBlock
, ImageChooserBlock
and DocumentChooserBlock
classes already implement a bulk_to_python()
method to support such optimisation.
Below is a generic block example that can be used to test this:
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core.blocks import CharBlock, ListBlock, StructBlock
class GalleryBlock(StructBlock):
heading = CharBlock(required=False)
images = ListBlock(ImageChooserBlock())
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top GitHub Comments
Thanks @chosak. I have renamed this issue to make it a little more generic, with the idea we continue to use it to track further progress.
@ababic @gasman I’ve merged #5930 in 3797132b4, which partially addresses the title of this issue although not the more general description and goal of more universal bulk_to_python support. Would it make sense to leave this open, or close and replace with another more generically-written issue?