Last active
July 17, 2018 02:12
-
-
Save MattSegal/a532203c86f94b52996f08e7cc4db143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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