queryset_reference.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. =======================
  2. Page QuerySet reference
  3. =======================
  4. All models that inherit from :class:`~wagtail.core.models.Page` are given some extra QuerySet methods accessible from their ``.objects`` attribute.
  5. Examples
  6. ========
  7. - Selecting only live pages
  8. .. code-block:: python
  9. live_pages = Page.objects.live()
  10. - Selecting published EventPages that are descendants of events_index
  11. .. code-block:: python
  12. events = EventPage.objects.live().descendant_of(events_index)
  13. - Getting a list of menu items
  14. .. code-block:: python
  15. # This gets a QuerySet of live children of the homepage with ``show_in_menus`` set
  16. menu_items = homepage.get_children().live().in_menu()
  17. Reference
  18. =========
  19. .. automodule:: wagtail.core.query
  20. .. autoclass:: PageQuerySet
  21. .. automethod:: live
  22. Example:
  23. .. code-block:: python
  24. published_pages = Page.objects.live()
  25. .. automethod:: not_live
  26. Example:
  27. .. code-block:: python
  28. unpublished_pages = Page.objects.not_live()
  29. .. automethod:: in_menu
  30. Example:
  31. .. code-block:: python
  32. # Build a menu from live pages that are children of the homepage
  33. menu_items = homepage.get_children().live().in_menu()
  34. .. note::
  35. To put your page in menus, set the show_in_menus flag to true:
  36. .. code-block:: python
  37. # Add 'my_page' to the menu
  38. my_page.show_in_menus = True
  39. .. automethod:: not_in_menu
  40. .. automethod:: in_site
  41. Example:
  42. .. code-block:: python
  43. # Get all the EventPages in the current site
  44. site = Site.find_for_request(request)
  45. site_events = EventPage.objects.in_site(site)
  46. .. automethod:: page
  47. Example:
  48. .. code-block:: python
  49. # Append an extra page to a QuerySet
  50. new_queryset = old_queryset | Page.objects.page(page_to_add)
  51. .. automethod:: not_page
  52. Example:
  53. .. code-block:: python
  54. # Remove a page from a QuerySet
  55. new_queryset = old_queryset & Page.objects.not_page(page_to_remove)
  56. .. automethod:: descendant_of
  57. Example:
  58. .. code-block:: python
  59. # Get EventPages that are under the special_events Page
  60. special_events = EventPage.objects.descendant_of(special_events_index)
  61. # Alternative way
  62. special_events = special_events_index.get_descendants()
  63. .. automethod:: not_descendant_of
  64. Example:
  65. .. code-block:: python
  66. # Get EventPages that are not under the archived_events Page
  67. non_archived_events = EventPage.objects.not_descendant_of(archived_events_index)
  68. .. automethod:: child_of
  69. Example:
  70. .. code-block:: python
  71. # Get a list of sections
  72. sections = Page.objects.child_of(homepage)
  73. # Alternative way
  74. sections = homepage.get_children()
  75. .. automethod:: not_child_of
  76. .. automethod:: ancestor_of
  77. Example:
  78. .. code-block:: python
  79. # Get the current section
  80. current_section = Page.objects.ancestor_of(current_page).child_of(homepage).first()
  81. # Alternative way
  82. current_section = current_page.get_ancestors().child_of(homepage).first()
  83. .. automethod:: not_ancestor_of
  84. Example:
  85. .. code-block:: python
  86. # Get the other sections
  87. other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage)
  88. .. automethod:: parent_of
  89. .. automethod:: not_parent_of
  90. .. automethod:: sibling_of
  91. Example:
  92. .. code-block:: python
  93. # Get list of siblings
  94. siblings = Page.objects.sibling_of(current_page)
  95. # Alternative way
  96. siblings = current_page.get_siblings()
  97. .. automethod:: not_sibling_of
  98. .. automethod:: public
  99. See: :ref:`private_pages`
  100. .. note::
  101. This doesn't filter out unpublished pages. If you want to only have published public pages, use ``.live().public()``
  102. Example:
  103. .. code-block:: python
  104. # Find all the pages that are viewable by the public
  105. all_pages = Page.objects.live().public()
  106. .. automethod:: not_public
  107. .. automethod:: search
  108. See: :ref:`wagtailsearch_searching_pages`
  109. Example:
  110. .. code-block:: python
  111. # Search future events
  112. results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello")
  113. .. automethod:: type
  114. Example:
  115. .. code-block:: python
  116. # Find all pages that are of type AbstractEmailForm, or one of it's subclasses
  117. form_pages = Page.objects.type(AbstractEmailForm)
  118. # Find all pages that are of type AbstractEmailForm or AbstractEventPage, or one of their subclasses
  119. form_and_event_pages = Page.objects.type(AbstractEmailForm, AbstractEventPage)
  120. .. automethod:: not_type
  121. .. automethod:: exact_type
  122. Example:
  123. .. code-block:: python
  124. # Find all pages that are of the exact type EventPage
  125. event_pages = Page.objects.exact_type(EventPage)
  126. # Find all page of the exact type EventPage or NewsPage
  127. news_and_events_pages = Page.objects.exact_type(EventPage, NewsPage)
  128. .. note::
  129. If you are only interested in pages of a single type, it is clearer (and often more efficient) to use
  130. the specific model's manager to get a queryset. For example:
  131. .. code-block:: python
  132. event_pages = EventPage.objects.all()
  133. .. automethod:: not_exact_type
  134. Example:
  135. .. code-block:: python
  136. # First, find all news and event pages
  137. news_and_events = Page.objects.type(NewsPage, EventPage)
  138. # Now exclude pages with an exact type of EventPage or NewsPage,
  139. # leaving only instance of more 'specialist' types
  140. specialised_news_and_events = news_and_events.not_exact_type(NewsPage, EventPage)
  141. .. automethod:: unpublish
  142. Example:
  143. .. code-block:: python
  144. # Unpublish current_page and all of its children
  145. Page.objects.descendant_of(current_page, inclusive=True).unpublish()
  146. .. automethod:: specific
  147. Example:
  148. .. code-block:: python
  149. # Get the specific instance of all children of the hompage,
  150. # in a minimum number of database queries.
  151. homepage.get_children().specific()
  152. See also: :py:attr:`Page.specific <wagtail.core.models.Page.specific>`
  153. .. automethod:: defer_streamfields
  154. Example:
  155. .. code-block:: python
  156. # Apply to a queryset to avoid fetching StreamField values
  157. # for a specific model
  158. EventPage.objects.all().defer_streamfields()
  159. # Or combine with specific() to avoid fetching StreamField
  160. # values for all models
  161. homepage.get_children().defer_streamfields().specific()
  162. .. automethod:: first_common_ancestor