onespacemedia / cms

A collection of Django extensions that add content-management facilities to Django projects.
BSD 3-Clause "New" or "Revised" License
14 stars 7 forks source link

Remove a pessimisation in PageManager #191

Closed lewiscollard closed 4 years ago

lewiscollard commented 4 years ago

Previously, getting the homepage from PageManager did this:

    def get_homepage(self):
        return self.prefetch_related('child_set__child_set').get(parent=None, is_content_object=False)

The intent is sound: get the children and all of their children in a single query. The only time you're interested in the homepage's children, is when you are rendering the page tree, and you would (should) always do that by accessing the .children cached property. But .children does this:

            for child in self.child_set.all().filter(is_content_object=False):
                child.parent = self
                children.append(child)

...which causes the prefetch to be invalidated by the is_content_object=False filter. Thus, this is actually the opposite of an optimisation; it actually adds three queries (and about 20ms cold-cache load).