2015-05-18 16:12:19 +02:00
=======================
Page QuerySet reference
=======================
2014-07-23 11:11:50 +02:00
2015-05-18 16:02:49 +02:00
All models that inherit from :class: `~wagtail.wagtailcore.models.Page` are given some extra QuerySet methods accessible from their `` .objects `` attribute.
2014-07-23 11:11:50 +02:00
Examples
========
- Selecting only live pages
.. code-block :: python
live_pages = Page.objects.live()
- Selecting published EventPages that are descendants of events_index
.. code-block :: python
events = EventPage.objects.live().descendant_of(events_index)
- Getting a list of menu items
.. code-block :: python
2015-04-18 00:05:08 +02:00
# This gets a QuerySet of live children of the homepage with `` show_in_menus `` set
2014-07-23 11:11:50 +02:00
menu_items = homepage.get_children().live().in_menu()
Reference
=========
.. automodule :: wagtail.wagtailcore.query
.. autoclass :: PageQuerySet
.. automethod :: live
Example:
.. code-block :: python
published_pages = Page.objects.live()
.. automethod :: not_live
Example:
.. code-block :: python
unpublished_pages = Page.objects.not_live()
.. automethod :: in_menu
Example:
.. code-block :: python
# 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:
.. code-block :: python
# Add 'my_page' to the menu
my_page.show_in_menus = True
2015-11-30 23:16:08 +01:00
.. automethod :: not_in_menu
2014-07-23 11:11:50 +02:00
.. automethod :: page
Example:
.. code-block :: python
2015-04-18 00:05:08 +02:00
# Append an extra page to a QuerySet
2014-07-23 11:11:50 +02:00
new_queryset = old_queryset | Page.objects.page(page_to_add)
.. automethod :: not_page
Example:
.. code-block :: python
2015-04-18 00:05:08 +02:00
# Remove a page from a QuerySet
2014-07-23 11:11:50 +02:00
new_queryset = old_queryset & Page.objects.not_page(page_to_remove)
.. automethod :: descendant_of
Example:
.. code-block :: python
# 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()
.. automethod :: not_descendant_of
Example:
.. code-block :: python
# Get EventPages that are not under the archived_events Page
non_archived_events = EventPage.objects.not_descendant_of(archived_events_index)
.. automethod :: child_of
Example:
.. code-block :: python
# Get a list of sections
sections = Page.objects.child_of(homepage)
# Alternative way
sections = homepage.get_children()
2015-11-30 23:16:08 +01:00
.. automethod :: not_child_of
2014-07-23 11:11:50 +02:00
.. automethod :: ancestor_of
Example:
.. code-block :: python
# 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()
.. automethod :: not_ancestor_of
Example:
.. code-block :: python
# Get the other sections
other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage)
2015-11-30 23:16:08 +01:00
.. automethod :: parent_of
.. automethod :: not_parent_of
2014-07-23 11:11:50 +02:00
.. automethod :: sibling_of
Example:
.. code-block :: python
# Get list of siblings
siblings = Page.objects.sibling_of(current_page)
# Alternative way
siblings = current_page.get_siblings()
2015-11-30 23:16:08 +01:00
.. automethod :: not_sibling_of
2014-07-23 11:11:50 +02:00
.. automethod :: public
See: :ref: `private_pages`
.. note ::
This doesn't filter out unpublished pages. If you want to only have published public pages, use `` .live().public() ``
Example:
.. code-block :: python
# Find all the pages that are viewable by the public
all_pages = Page.objects.live().public()
2015-11-30 23:16:08 +01:00
.. automethod :: not_public
2014-07-23 11:11:50 +02:00
.. automethod :: search
2014-09-28 11:11:34 +02:00
See: :ref: `wagtailsearch_searching_pages`
2014-07-23 11:11:50 +02:00
Example:
.. code-block :: python
# Search future events
results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello")
2014-10-01 18:37:38 +02:00
.. automethod :: type
Example:
.. code-block :: python
# Find all pages that are of type AbstractEmailForm, or a descendant of it
form_pages = Page.objects.type(AbstractEmailForm)
2014-10-04 18:45:50 +02:00
2015-11-30 23:16:08 +01:00
.. automethod :: not_type
2015-11-30 21:27:10 +01:00
.. automethod :: exact_type
Example:
.. code-block :: python
# Find all pages that are of the exact type EventPage
event_pages = Page.objects.exact_type(EventPage)
.. automethod :: not_exact_type
Example:
.. code-block :: python
# Find all pages that are not of the exact type EventPage (but may be a subclass)
non_event_pages = Page.objects.not_exact_type(EventPage)
2014-10-04 18:45:50 +02:00
.. automethod :: unpublish
Example:
.. code-block :: python
# Unpublish current_page and all of its children
Page.objects.descendant_of(current_page, inclusive=True).unpublish()
2015-04-30 02:04:59 +02:00
.. automethod :: specific
Example:
.. code-block :: python
# Get the specific instance of all children of the hompage,
# in a minimum number of database queries.
homepage.get_children().specific()
See also: :py:attr: `Page.specific <wagtail.wagtailcore.models.Page.specific>`
2016-10-12 01:26:58 +02:00
.. automethod :: first_common_ancestor