queryset_reference.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. =======================
  2. Page QuerySet reference
  3. =======================
  4. All models that inherit from :class:`~wagtail.wagtailcore.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.wagtailcore.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_events = EventPage.objects.in_site(request.site)
  45. .. automethod:: page
  46. Example:
  47. .. code-block:: python
  48. # Append an extra page to a QuerySet
  49. new_queryset = old_queryset | Page.objects.page(page_to_add)
  50. .. automethod:: not_page
  51. Example:
  52. .. code-block:: python
  53. # Remove a page from a QuerySet
  54. new_queryset = old_queryset & Page.objects.not_page(page_to_remove)
  55. .. automethod:: descendant_of
  56. Example:
  57. .. code-block:: python
  58. # Get EventPages that are under the special_events Page
  59. special_events = EventPage.objects.descendant_of(special_events_index)
  60. # Alternative way
  61. special_events = special_events_index.get_descendants()
  62. .. automethod:: not_descendant_of
  63. Example:
  64. .. code-block:: python
  65. # Get EventPages that are not under the archived_events Page
  66. non_archived_events = EventPage.objects.not_descendant_of(archived_events_index)
  67. .. automethod:: child_of
  68. Example:
  69. .. code-block:: python
  70. # Get a list of sections
  71. sections = Page.objects.child_of(homepage)
  72. # Alternative way
  73. sections = homepage.get_children()
  74. .. automethod:: not_child_of
  75. .. automethod:: ancestor_of
  76. Example:
  77. .. code-block:: python
  78. # Get the current section
  79. current_section = Page.objects.ancestor_of(current_page).child_of(homepage).first()
  80. # Alternative way
  81. current_section = current_page.get_ancestors().child_of(homepage).first()
  82. .. automethod:: not_ancestor_of
  83. Example:
  84. .. code-block:: python
  85. # Get the other sections
  86. other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage)
  87. .. automethod:: parent_of
  88. .. automethod:: not_parent_of
  89. .. automethod:: sibling_of
  90. Example:
  91. .. code-block:: python
  92. # Get list of siblings
  93. siblings = Page.objects.sibling_of(current_page)
  94. # Alternative way
  95. siblings = current_page.get_siblings()
  96. .. automethod:: not_sibling_of
  97. .. automethod:: public
  98. See: :ref:`private_pages`
  99. .. note::
  100. This doesn't filter out unpublished pages. If you want to only have published public pages, use ``.live().public()``
  101. Example:
  102. .. code-block:: python
  103. # Find all the pages that are viewable by the public
  104. all_pages = Page.objects.live().public()
  105. .. automethod:: not_public
  106. .. automethod:: search
  107. See: :ref:`wagtailsearch_searching_pages`
  108. Example:
  109. .. code-block:: python
  110. # Search future events
  111. results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello")
  112. .. automethod:: type
  113. Example:
  114. .. code-block:: python
  115. # Find all pages that are of type AbstractEmailForm, or a descendant of it
  116. form_pages = Page.objects.type(AbstractEmailForm)
  117. .. automethod:: not_type
  118. .. automethod:: exact_type
  119. Example:
  120. .. code-block:: python
  121. # Find all pages that are of the exact type EventPage
  122. event_pages = Page.objects.exact_type(EventPage)
  123. .. automethod:: not_exact_type
  124. Example:
  125. .. code-block:: python
  126. # Find all pages that are not of the exact type EventPage (but may be a subclass)
  127. non_event_pages = Page.objects.not_exact_type(EventPage)
  128. .. automethod:: unpublish
  129. Example:
  130. .. code-block:: python
  131. # Unpublish current_page and all of its children
  132. Page.objects.descendant_of(current_page, inclusive=True).unpublish()
  133. .. automethod:: specific
  134. Example:
  135. .. code-block:: python
  136. # Get the specific instance of all children of the hompage,
  137. # in a minimum number of database queries.
  138. homepage.get_children().specific()
  139. See also: :py:attr:`Page.specific <wagtail.wagtailcore.models.Page.specific>`
  140. .. automethod:: first_common_ancestor