Page Queryset Methods¶
All models that inherit from Page
are given some extra Queryset methods accessible from their .objects
attribute.
Examples¶
Selecting only live pages
live_pages = Page.objects.live()Selecting published EventPages that are descendants of events_index
events = EventPage.objects.live().descendant_of(events_index)Getting a list of menu items
# This gets a queryset of live children of the homepage with ``show_in_menus`` set menu_items = homepage.get_children().live().in_menu()
Reference¶
-
class
wagtail.wagtailcore.query.
PageQuerySet
(model=None, query=None, using=None)¶ -
live
()¶ This filters the queryset to only contain published pages.
Example:
published_pages = Page.objects.live()
-
not_live
()¶ This filters the queryset to only contain unpublished pages.
Example:
unpublished_pages = Page.objects.not_live()
This filters the queryset to only contain pages that are in the menus.
Example:
# Build a menu from live pages that are children of the homepage menu_items = homepage.get_children().live().in_menu()
Note
To put your page in menus, set the show_in_menus flag to true:
# Add 'my_page' to the menu my_page.show_in_menus = True
-
page
(other)¶ This filters the queryset so it only contains the specified page.
Example:
# Append an extra page to a queryset new_queryset = old_queryset | Page.objects.page(page_to_add)
-
not_page
(other)¶ This filters the queryset so it doesn’t contain the specified page.
Example:
# Remove a page from a queryset new_queryset = old_queryset & Page.objects.not_page(page_to_remove)
-
descendant_of
(other, inclusive=False)¶ This filters the queryset to only contain pages that descend from the specified page.
If inclusive is set to True, it will also contain the page itself (instead of just its descendants).
Example:
# Get EventPages that are under the special_events Page special_events = EventPage.objects.descendant_of(special_events_index) # Alternative way special_events = special_events_index.get_descendants()
-
not_descendant_of
(other, inclusive=False)¶ This filters the queryset to not contain any pages that descend from the specified page.
If inclusive is set to True, it will also exclude the specified page.
Example:
# Get EventPages that are not under the archived_events Page non_archived_events = EventPage.objects.not_descendant_of(archived_events_index)
-
child_of
(other)¶ This filters the queryset to only contain pages that are direct children of the specified page.
Example:
# Get a list of sections sections = Page.objects.child_of(homepage) # Alternative way sections = homepage.get_children()
-
ancestor_of
(other, inclusive=False)¶ This filters the queryset to only contain pages that are ancestors of the specified page.
If inclusive is set to True, it will also include the specified page.
Example:
# Get the current section current_section = Page.objects.ancestor_of(current_page).child_of(homepage).first() # Alternative way current_section = current_page.get_ancestors().child_of(homepage).first()
-
not_ancestor_of
(other, inclusive=False)¶ This filters the queryset to not contain any pages that are ancestors of the specified page.
If inclusive is set to True, it will also exclude the specified page.
Example:
# Get the other sections other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage)
-
sibling_of
(other, inclusive=True)¶ This filters the queryset to only contain pages that are siblings of the specified page.
By default, inclusive is set to True so it will include the specified page in the results.
If inclusive is set to False, the page will be excluded from the results.
Example:
# Get list of siblings siblings = Page.objects.sibling_of(current_page) # Alternative way siblings = current_page.get_siblings()
-
public
()¶ This filters the queryset to only contain pages that are not in a private section
See: Private pages
Note
This doesn’t filter out unpublished pages. If you want to only have published public pages, use
.live().public()
Example:
# Find all the pages that are viewable by the public all_pages = Page.objects.live().public()
-
search
(query_string, fields=None, backend='default')¶ This runs a search query on all the pages in the queryset
See: Searching Pages
Example:
# Search future events results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello")
-
type
(model)¶ This filters the queryset to only contain pages that are an instance of the specified model (including subclasses).
Example:
# Find all pages that are of type AbstractEmailForm, or a descendant of it form_pages = Page.objects.type(AbstractEmailForm)
-
unpublish
()¶ This unpublishes all pages in the queryset
Example:
# Unpublish current_page and all of its children Page.objects.descendant_of(current_page, inclusive=True).unpublish()
-