Skip to content

Instantly share code, notes, and snippets.

@MattSegal
Last active July 17, 2018 02:12
Show Gist options
  • Save MattSegal/a532203c86f94b52996f08e7cc4db143 to your computer and use it in GitHub Desktop.
Save MattSegal/a532203c86f94b52996f08e7cc4db143 to your computer and use it in GitHub Desktop.
class ArticlePage(Page):
pass
class SectionPage(Page):
# ...
def route(self, request, path_components):
"""
Perform ORM optimisations on ArticlePage to speed up article load times
"""
try:
subpage = self.get_children_for_render().get(slug=path_components[0])
return subpage.specific.route(request, path_components[1:])
except (Page.DoesNotExist, Http404):
pass
def get_children_for_render(self):
if self.is_leaf():
return ArticlePage.objects.none()
return (
ArticlePage.objects.live().public()
.select_related('author')
.select_related('company')
.select_related('company__logo')
# Article body images
.prefetch_related('images')
.prefetch_related('images__renditions')
# Equivalent to treebeard's mp_tree.MP_Node.get_children()
.filter(
depth=self.depth + 1,
path__range=self._get_children_path_interval(self.path)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment