# Page QuerySet 参考 所有继承 {class}`~wagtail.models.AbstractPage` 的模型,其 `.objects` 属性都会额外提供一些 QuerySet 方法。 ## 示例 ### 只选出线上页面 ```python live_pages = Page.objects.live() ``` ### 选出 events_index 的后代中已发布的 EventPage ```python events = EventPage.objects.live().descendant_of(events_index) ``` ### 获取菜单项列表 ```python # 这会得到一个 QuerySet,包含首页的线上子页面中设置了 ``show_in_menus`` 的那些 menu_items = homepage.get_children().live().in_menu() ``` ## 参考 ```{eval-rst} .. automodule:: wagtail.query .. autoclass:: PageQuerySet .. automethod:: live 示例: .. code-block:: python published_pages = Page.objects.live() .. automethod:: not_live 示例: .. code-block:: python unpublished_pages = Page.objects.not_live() .. automethod:: in_menu 示例: .. code-block:: python # 用首页的线上子页面构建一个菜单 menu_items = homepage.get_children().live().in_menu() .. note:: 要把页面放进菜单,请把 show_in_menus 标志设为 true: .. code-block:: python # 把 'my_page' 加入菜单 my_page.show_in_menus = True .. automethod:: not_in_menu .. automethod:: in_site 示例: .. code-block:: python # 获取当前站点中的所有 EventPage site = Site.find_for_request(request) site_events = EventPage.objects.in_site(site) .. automethod:: page 示例: .. code-block:: python # 往 QuerySet 中追加一个额外页面 new_queryset = old_queryset | Page.objects.page(page_to_add) .. automethod:: not_page 示例: .. code-block:: python # 从 QuerySet 中移除一个页面 new_queryset = old_queryset & Page.objects.not_page(page_to_remove) .. automethod:: descendant_of 示例: .. code-block:: python # 获取 special_events 页面下的 EventPage special_events = EventPage.objects.descendant_of(special_events_index) # 另一种方式 special_events = special_events_index.get_descendants() .. automethod:: not_descendant_of 示例: .. code-block:: python # 获取不在 archived_events 页面下的 EventPage non_archived_events = EventPage.objects.not_descendant_of(archived_events_index) .. automethod:: child_of 示例: .. code-block:: python # 获取各栏目列表 sections = Page.objects.child_of(homepage) # 另一种方式 sections = homepage.get_children() .. automethod:: not_child_of .. automethod:: ancestor_of 示例: .. code-block:: python # 获取当前栏目 current_section = Page.objects.ancestor_of(current_page).child_of(homepage).first() # 另一种方式 current_section = current_page.get_ancestors().child_of(homepage).first() .. automethod:: not_ancestor_of 示例: .. code-block:: python # 获取其他栏目 other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage) .. automethod:: parent_of .. automethod:: not_parent_of .. automethod:: sibling_of 示例: .. code-block:: python # 获取兄弟页面列表 siblings = Page.objects.sibling_of(current_page) # 另一种方式 siblings = current_page.get_siblings() .. automethod:: not_sibling_of .. automethod:: public 参见::ref:`private_pages` .. note:: 这不会过滤掉未发布的页面。如果你只想得到已发布的公开页面,请使用 ``.live().public()`` 示例: .. code-block:: python # 找出所有公众可查看的页面 all_pages = Page.objects.live().public() .. automethod:: not_public .. automethod:: private .. automethod:: search 参见::ref:`wagtailsearch_searching_pages` 示例: .. code-block:: python # 搜索未来的事件 results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello") .. automethod:: type 示例: .. code-block:: python # 找出所有属于 AbstractEmailForm 类型或其子类的页面 form_pages = Page.objects.type(AbstractEmailForm) # 找出所有属于 AbstractEmailForm 或 AbstractEventPage 类型或其子类的页面 form_and_event_pages = Page.objects.type(AbstractEmailForm, AbstractEventPage) .. automethod:: not_type .. automethod:: exact_type 示例: .. code-block:: python # 找出所有恰好是 EventPage 类型的页面 event_pages = Page.objects.exact_type(EventPage) # 找出所有恰好是 EventPage 或 NewsPage 类型的页面 news_and_events_pages = Page.objects.exact_type(EventPage, NewsPage) .. note:: 如果你只关心某一种类型的页面,用该具体模型的管理器获取 queryset 会更清晰(通常也更高效)。例如: .. code-block:: python event_pages = EventPage.objects.all() .. automethod:: not_exact_type 示例: .. code-block:: python # 首先,找出所有新闻和事件页面 news_and_events = Page.objects.type(NewsPage, EventPage) # 现在排除恰好为 EventPage 或 NewsPage 类型的页面, # 只剩下更“专业”的类型的实例 specialised_news_and_events = news_and_events.not_exact_type(NewsPage, EventPage) .. automethod:: unpublish 示例: .. code-block:: python # 取消发布 current_page 及其所有子页面 Page.objects.descendant_of(current_page, inclusive=True).unpublish() .. automethod:: specific 示例: .. code-block:: python # 以最少的数据库查询,获取首页所有子页面的 # specific 实例。 homepage.get_children().specific() 另见::py:attr:`Page.specific ` .. automethod:: defer_streamfields 示例: .. code-block:: python # 应用到 queryset 上,避免为某个特定模型 # 获取 StreamField 值 EventPage.objects.all().defer_streamfields() # 或与 specific() 组合使用,避免为所有模型 # 获取 StreamField 值 homepage.get_children().defer_streamfields().specific() .. automethod:: first_common_ancestor .. automethod:: select_related .. automethod:: prefetch_related .. note:: 通常使用 ``prefetch_related()`` 会为每个提供的 ``lookups`` 额外执行一次数据库查询。 不过当它与 ``for_specific_subqueries=True`` 组合时,这个额外查询数 会按结果中的每种具体类型成倍增加。如果你只获取 少量对象,或者结果的类型差异很可能很大, 那么执行这些额外查询带来的额外开销实际上可能对性能 产生负面影响。 把 ``prefetch_related()`` 与 ``for_specific_subqueries=True`` 一起使用,应当保留 给需要大量结果的场合,或者类型差异 在某种程度上受限的场合。例如,在渲染子页面列表时, 父页面上设置了 ``allow_subtypes``,把结果限制在少量 页面类型上。或者,已经应用了 ``type()`` 或 ``not_type()`` 过滤, 把 queryset 限制在少量具体类型上。 ```