New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

How to only get Parent and Child Class Pages and their Specific Fields

When querying for a Wagtail Page that has child (Subclassed) pages, you'll receive all of the pages in your QuerySet. There are times when you simply want the child pages (all types), a specific type of child page, or just the parent page itself with no children. In this lesson we'll explore Wagtails .not_exact_type() and .exact_type() methods.

There are several ways to query your Wagtail Pages and return QuerySets containing only the page classes you want. Below is some example code.

Just Parent Pages

This example assumes that there are child (subclassed) pages that inherit form BlogDetailPage.

``` parent_pages = ```
Just Child Pages

This example assumes each child page is inheriting from BlogDetailPage. This will return a QuerySet of BlogDetailPage's, not the actual class you'd be expecting.

``` child_pages = ```
Just Specific Child Pages

If you're trying to query for all child pages that inherit from a parent page (ie. BlogDetailPage), you'll receive a QuerySet of BlogDetailPage's. To get the specific child pages, append .specific() to your query.

``` child_pages = ```
Disable Property Deferring

Wagtail is built for speed and efficiency. It's easy to get away from that and write code that performs terribly. So by default the .specific() argument called defer is set to True (.specific(defer=True)). Grabbing specific fields from a model means adding another database hit, and if the QuerySet is large, this can become a problem. So it's good that specific property deferring is enabled by default.

But occasionally you want the specific properties of a class. To disabled property deferring simply append .specific(defer=False) to your ORM Query.

Full Wagtail QuerySet Reference

Wagtail comes with a lot of different QuerySet options. This lesson only went over 2 of them, but there are many more. Definitely take a couple minutes and read through their docs page.

The Git Commit

This lesson works purely in the Django Shell and doesn't have any code to committed to the repo. Above is where you can find example code.

Related tutorials

Restricting Parent and Child Pages

Posted on

When you start to create several different page types in Wagtail and you create a child page (a page that's nested under a different page) you'll see every single page is an option. In this video we'll explore how to restrict where pages can live by telling the parent page what types of child page(s) they should expect, and by telling the child pages which parent page(s) they can expect.

View lesson, Restricting Parent and Child Pages

Getting Child Page Properties From a Subclassed Page

Posted on

In Wagtail, and just like in Django, you can subclass classes. In this lesson, we're subclassing a Wagtail Page into 2 child pages. But when we query for all the parent classes, we're also given the child classes in the QuerySet, and the data is somewhat inconsistent because child classes can have unique fields that differ from their parents and siblings.

View lesson, Getting Child Page Properties From a Subclassed Page

Headless CMS: Serializing Child Pages (and QuerySets)

Posted on

In this video we'll add all the blog pages to the blog listing page API response. This can be very helpful for Listing-style pages in your Headless Wagtail website.

View lesson, Headless CMS: Serializing Child Pages (and QuerySets)

How to Subclass Wagtail Pages

Posted on

Subclassing is having a class (in this case it's a Wagtail Page) that can be used for other classes (Wagtail Page's). The parent class has all the common attributes for the child pages, and every child page will inherit everything from it's parent. In this lesson we'll explore that by creating a subclassed Article and Video Blog Page that share a common parent, and then we'll extend the functionality of both subclassed pages by adding new fields.

View lesson, How to Subclass Wagtail Pages

The Ultimate Wagtail Developers Course

This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.

Ultimate Wagtail Developers Course Logo