[Django]-How to query subclasses of Page in wagtail?

4👍

You should use the specific attribute to get the most specific subclassed form of each page:

http://docs.wagtail.io/en/v1.8/reference/pages/model_reference.html#wagtail.wagtailcore.models.Page.specific

Also, there’s a specific() method on PageQuerySet which can also be called to retrieve the specific version of all Pages in that queryset.

Finally, notice that calling specific on a page or queryset will result to an extra query so try to group all specific calls together. For example, instead of doing:

for page in pages:
    specific_page = page.specific # This will result to N extra queries (!)
    # do something with specific_page

do a

for specigic_page in pages.specific(): # 1 extra query for each page subclass
    # do something with specific_page

Leave a comment