Accueil / Blog / Métier / 2016 / Wagtail: How to use the Page model and its manager (part 2)

Wagtail: How to use the Page model and its manager (part 2)

Par Yann Fouillat — publié 08/08/2016
The Page model has several methods specific to Wagtail. This is also the case of its manager. We will see this thoroughly in this post.
Wagtail: How to use the Page model and its manager (part 2)

Wagtail

Previous part.

Manager and QuerySet

First, it is important to understand something: every content type inherits from Page. It is obviously possible to get only one content type by using its own manager directly. But it is also possible to get every pages, whichever type they have, by using the Page manager:

BlogPage.objects.filter(slug="page")  # Will return a QuerySet containing BlogPages
Page.objects.fitler(slug="page")  # Will return a QuerySet containing Pages

Thus, by using the Page manager, we will not get an instance of our specific model, but of Page. This means that we won't have access to its own methods and attributes. Fortunately, Wagtail offers us a solution:

Page.objects.fitler(slug="page").specific()  # Will return instances of the specific content types rather than Pages.

For the rest of this post, I will only use the Page manager, but it is obviously possible to use the same methods on the managers of your own content types.

I will only talk about the important methods to know (the one you will have to use 90% of the time), for an exhaustive list, please see the documentation. Each method has an inverse version (not_*), which excludes the pages rather than filters on them. Each method also returns a QuerySet, which allows to chain them.

live() / not_live()

live filters the QuerySet to only contain the published pages.

descendant_of(page, inclusive=False) / not_descendant_of(page, inclusive=False)

Filter the QuerySet to only contain the child pages, recursively. If inclusive is True, the QuerySet will also contain the page passed.

Page.objects.descendant_of(blog_index)  # Will return all pages under this blog index

child_of(page) / not_child_of(page)

Filter the QuerySet to only contain the direct child pages. If inclusive is True, the QuerySet will also contain the page passed.

ancestor_of(page, inclusive=False) / not_ancestor_of(page, inclusive=False)

Filter the QuerySet to only contain the ancestor pages. This means the parent of the page, and its parent's parent, and so on. If inclusive is True, the QuerySet will also contain the page passed.

parent_of(page) / not_parent_of(page)

Filter the QuerySet to only contain the parent of the page.

sibling_of(page, inclusive=True) / not_sibling_of(page, inclusive=True)

Filter the QuerySet to only contain the sibling pages. If inclusive is True, the QuerySet will also contain the page passed.

public() / not_public()

Filter the QuerySet to only contain the pages which are not in a private section.

type(model) / not_type(model)

Filter the QuerySet to only contain the pages of the type passed.

Page.objects.type(BlogPage)  # Will return all blog pages as Pages

Custom Manager

You can customize your content type managers by inheriting from the PageManager class, or by creating your own QuerySet by inheriting from PageQueryset and by using the from_queryset() method. See the documentation for more information.

The Page model

The page instances also have interesting methods and attributes to know. Once again, the exhaustive list is available in the documentation.

specific

Return an instance of the actual class of the page.

blog_index.specific()  # Return an instance of BlogIndex even if blog_index is a Page.

specific_class

Return the actual class of the instance.

blog_index.specific_class()  # Return BlogIndex

url

Return the relative url of the page.

full_url

Return the absolute url of the page.

get_ancestors(inclusive=False)

Return a QuerySet of the ancestors of the page. It is similar to the QuerySet method:

page.get_ancestors()
Page.objects.ancestor_of(page)

get_descendants(inclusive=False)

Return a QuerySet of the descendants of the page. It is similar to the QuerySet method:

page.get_descendants()
Page.objects.descendant_of(page)

get_siblings(inclusive=True)

Return a QuerySet of the siblings of the page. It is similar to the QuerySet method:

page.get_siblings()
Page.objects.sibling_of(page)

Conclusion

Now that you know how to use Pages, we are going to see how to display them. Time for the templates in the third part!

ABONNEZ-VOUS À LA NEWSLETTER !
Voir aussi
Internationalisation avec Django Internationalisation avec Django 27/11/2018

En tant que développeurs nous sommes parfois confronté à la problématique de l'accessibilité ...

Python : Bien configurer son environnement de développement Python : Bien configurer son environnement de développement 07/12/2015

Comment utiliser les bonnes pratiques de développement Python.

Formation Django initiation à Toulouse du 13 au 15 mars Formation Django initiation à Toulouse du 13 au 15 mars 26/01/2017

Entrez de plain-pied dans l'univers de Django aux côtés de développeurs ayant une expérience de ...

Retour sur la PyConFr 2016 Retour sur la PyConFr 2016 18/10/2016

Nous étions présents à Rennes pour PyConFr 2016. Voici notre compte-rendu à chaud.

Wagtail : How to make your own content type models (part 1) Wagtail : How to make your own content type models (part 1) 29/07/2016

We are used to initialize our CMS directly from a web interface, often including lots of complex ...