question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StreamField should consistently use bulk_to_python() to improve efficiency

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ababiccommented, Apr 3, 2020

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.

1reaction
chosakcommented, Apr 3, 2020

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use StreamField for mixed content
Using StreamField ¶ · BlogPage is defined as a · StreamField where authors can compose content from three different block types: headings, paragraphs,...
Read more >
Freeform page content using StreamField — Wagtail 2.2.2 ...
This defines the set of available block types that can be used within this field. The author of the page is free to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found