페이지 QuerySet 참조¶
Page 에서 상속하는 모든 모델은 .objects 속성에서 액세스할 수 있는 추가 QuerySet 메서드를 제공합니다.
예시¶
라이브 페이지만 선택¶
live_pages = Page.objects.live()
events_index의 하위인 게시된 EventPage 선택¶
events = EventPage.objects.live().descendant_of(events_index)
메뉴 항목 목록 가져오기¶
# 이것은 `show_in_menus` 가 설정된 홈페이지의 라이브 자식 QuerySet을 가져옵니다.
menu_items = homepage.get_children().live().in_menu()
참조¶
- class wagtail.query.PageQuerySet(*args, **kwargs)¶
- live()¶
This filters the QuerySet to only contain published pages.
예시:
published_pages = Page.objects.live()
- not_live()¶
This filters the QuerySet to only contain unpublished pages.
예시:
unpublished_pages = Page.objects.not_live()
This filters the QuerySet to only contain pages that are in the menus.
예시:
# 홈페이지의 자식인 라이브 페이지에서 메뉴 빌드 menu_items = homepage.get_children().live().in_menu()
참고
페이지를 메뉴에 넣으려면 show_in_menus 플래그를 true로 설정하십시오.
# 'my_page'를 메뉴에 추가 my_page.show_in_menus = True
This filters the QuerySet to only contain pages that are not in the menus.
- in_site(site)¶
This filters the QuerySet to only contain pages within the specified site.
예시:
# 현재 사이트의 모든 EventPage 가져오기 site = Site.find_for_request(request) site_events = EventPage.objects.in_site(site)
- page(other)¶
This filters the QuerySet so it only contains the specified page.
예시:
# 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.
예시:
# 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).
예시:
# special_events Page 아래에 있는 EventPage 가져오기 special_events = EventPage.objects.descendant_of(special_events_index) # 다른 방법 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.
예시:
# archived_events Page 아래에 없는 EventPage 가져오기 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.
예시:
# 섹션 목록 가져오기 sections = Page.objects.child_of(homepage) # 다른 방법 sections = homepage.get_children()
- not_child_of(other)¶
This filters the QuerySet to not contain any pages that are direct children of the specified page.
- 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.
예시:
# 현재 섹션 가져오기 current_section = Page.objects.ancestor_of(current_page).child_of(homepage).first() # 다른 방법 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.
예시:
# 다른 섹션 가져오기 other_sections = Page.objects.not_ancestor_of(current_page).child_of(homepage)
- parent_of(other)¶
This filters the QuerySet to only contain the parent of the specified page.
- not_parent_of(other)¶
This filters the QuerySet to exclude the parent of the specified page.
- 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.
예시:
# 형제 목록 가져오기 siblings = Page.objects.sibling_of(current_page) # 다른 방법 siblings = current_page.get_siblings()
- not_sibling_of(other, inclusive=True)¶
This filters the QuerySet to not contain any pages that are siblings of the specified page.
By default, inclusive is set to True so it will exclude the specified page from the results.
If inclusive is set to False, the page will be included in the results.
- public()¶
Filters the QuerySet to only contain pages that are not in a private section and their descendants.
참조: 비공개 페이지
참고
게시되지 않은 페이지는 필터링하지 않습니다. 게시된 공개 페이지만 원하면
.live().public()을 사용하십시오.예시:
# 공개적으로 볼 수 있는 모든 페이지 찾기 all_pages = Page.objects.live().public()
- not_public()¶
Filters the QuerySet to only contain pages that are in a private section and their descendants.
- private()¶
Filters the QuerySet to only contain pages that are in a private section and their descendants.
- search(query, fields=None, operator=None, order_by_relevance=True, backend='default')¶
This runs a search query on all the items in the QuerySet
참조: QuerySet 검색
예시:
# 미래 이벤트 검색 results = EventPage.objects.live().filter(date__gt=timezone.now()).search("Hello")
- type(*types)¶
This filters the QuerySet to only contain pages that are an instance of the specified model(s) (including subclasses).
예시:
# AbstractEmailForm 또는 그 서브클래스 유형의 모든 페이지 찾기 form_pages = Page.objects.type(AbstractEmailForm) # AbstractEmailForm 또는 AbstractEventPage 또는 그 서브클래스 유형의 모든 페이지 찾기 form_and_event_pages = Page.objects.type(AbstractEmailForm, AbstractEventPage)
- not_type(*types)¶
This filters the QuerySet to exclude any pages which are an instance of the specified model(s).
- exact_type(*types)¶
This filters the QuerySet to only contain pages that are an instance of the specified model(s) (matching the model exactly, not subclasses).
예시:
# 정확히 EventPage 유형의 모든 페이지 찾기 event_pages = Page.objects.exact_type(EventPage) # 정확히 EventPage 또는 NewsPage 유형의 모든 페이지 찾기 news_and_events_pages = Page.objects.exact_type(EventPage, NewsPage)
참고
단일 유형의 페이지에만 관심이 있다면, 특정 모델의 관리자를 사용하여 쿼리셋을 가져오는 것이 더 명확하고(종종 더 효율적입니다). 예시:
event_pages = EventPage.objects.all()
- not_exact_type(*types)¶
This filters the QuerySet to exclude any pages which are an instance of the specified model(s) (matching the model exactly, not subclasses).
예시:
# 먼저 모든 뉴스 및 이벤트 페이지 찾기 news_and_events = Page.objects.type(NewsPage, EventPage) # 이제 EventPage 또는 NewsPage의 정확한 유형을 가진 페이지를 제외하고, # 더 '전문적인' 유형의 인스턴스만 남깁니다. specialised_news_and_events = news_and_events.not_exact_type(NewsPage, EventPage)
- unpublish()¶
This unpublishes all live pages in the QuerySet.
예시:
# current_page 및 모든 자식 게시 취소 Page.objects.descendant_of(current_page, inclusive=True).unpublish()
- specific(defer=False)¶
This efficiently gets all the specific items for the queryset, using the minimum number of queries.
When the “defer” keyword argument is set to True, only generic field values will be loaded and all specific fields will be deferred.
예시:
# 최소한의 데이터베이스 쿼리로 홈페이지의 모든 자식의 특정 인스턴스를 가져옵니다. homepage.get_children().specific()
참조:
Page.specific
- defer_streamfields()¶
Apply to a queryset to prevent fetching/decoding of StreamField values on evaluation. Useful when working with potentially large numbers of results, where StreamField values are unlikely to be needed. For example, when generating a sitemap or a long list of page links.
예시:
# 특정 모델에 대한 StreamField 값을 가져오지 않도록 쿼리셋에 적용 EventPage.objects.all().defer_streamfields() # 또는 모든 모델에 대한 StreamField 값을 가져오지 않도록 specific()과 결합 homepage.get_children().defer_streamfields().specific()
- first_common_ancestor(include_self=False, strict=False)¶
Find the first ancestor that all pages in this queryset have in common. For example, consider a page hierarchy like:
- Home/ - Foo Event Index/ - Foo Event Page 1/ - Foo Event Page 2/ - Bar Event Index/ - Bar Event Page 1/ - Bar Event Page 2/
The common ancestors for some queries would be:
>>> Page.objects\ ... .type(EventPage)\ ... .first_common_ancestor() <Page: Home> >>> Page.objects\ ... .type(EventPage)\ ... .filter(title__contains='Foo')\ ... .first_common_ancestor() <Page: Foo Event Index>
This method tries to be efficient, but if you have millions of pages scattered across your page tree, it will be slow.
If include_self is True, the ancestor can be one of the pages in the queryset:
>>> Page.objects\ ... .filter(title__contains='Foo')\ ... .first_common_ancestor() <Page: Foo Event Index> >>> Page.objects\ ... .filter(title__exact='Bar Event Index')\ ... .first_common_ancestor() <Page: Bar Event Index>
A few invalid cases exist: when the queryset is empty, when the root Page is in the queryset and
include_selfis False, and when there are multiple page trees with no common root (a case Wagtail does not support). Ifstrictis False (the default), then the first root node is returned in these cases. Ifstrictis True, then aObjectDoesNotExistis raised.
Overrides Django’s native
select_related()to allow related objects to be fetched by the subqueries made when a specific queryset is evaluated.When
for_specific_subqueriesisFalse(the default), the method functions exactly like the original method. However, whenTrue,fieldsare required, and must match names of ForeignKey fields on all specific models that might be included in the result (which can include fields inherited from concrete parents). Unlike whenfor_specific_subqueriesisFalse, no validation is applied tofieldswhen the method is called. Rather, that when the method is called. Instead, that validation is applied for each individual subquery when the queryset is evaluated. This difference in behaviour should be taken into account when experimenting withfor_specific_subqueries=True.As with Django’s native implementation, you chain multiple applications of
select_related()withfor_specific_subqueries=Trueto progressively add to the list of fields to be fetched. For example:# Fetch 'author' when retrieving specific page data queryset = Page.objects.specific().select_related("author", for_specific_subqueries=True) # We're rendering cards with images, so fetch the listing image too queryset = queryset.select_related("listing_image", for_specific_subqueries=True) # Fetch some key taxonomy data too queryset = queryset.select_related("topic", "target_audience", for_specific_subqueries=True)
As with Django’s native implementation,
Nonecan be supplied in place offieldsto negate a previous application ofselect_related(). By default, this will only work for cases whereselect_related()was called withoutfor_specific_subqueries, or withfor_specific_subqueries=False. However, you can usefor_specific_subqueries=Trueto negate subquery-specific applications too. For example:# Fetch 'author' and 'listing_image' when retrieving specific page data queryset = Page.objects.specific().select_related( "author", "listing_image", for_specific_subqueries=True ) # I've changed my mind. Do not fetch any additional data queryset = queryset.select_related(None, for_specific_subqueries=True)
Overrides Django’s native
prefetch_related()implementation to allow related objects to be fetched alongside the subqueries made when a specific queryset is evaluated.When
for_specific_subqueriesisFalse(the default), the method functions exactly like the original method. However, whenTrue,lookupsare required, and must match names of related fields on all specific models that might be included in the result (which can include relationships inherited from concrete parents). Unlike whenfor_specific_subqueriesisFalse, no validation is applied tolookupswhen the method is called. Instead, that validation is applied for each individual subquery when the queryset is evaluated. This difference in behaviour should be taken into account when experimenting withfor_specific_subqueries=True.As with Django’s native implementation, you chain multiple applications of
prefetch_related()withfor_specific_subqueries=Trueto progressively add to the list of lookups to be made. For example:# Fetch 'contributors' when retrieving specific page data queryset = Page.objects.specific().prefetch_related("contributors", for_specific_subqueries=True) # We're rendering cards with images, so prefetch listing image renditions too queryset = queryset.prefetch_related("listing_image__renditions", for_specific_subqueries=True) # Fetch some key taxonomy data also queryset = queryset.prefetch_related("tags", for_specific_subqueries=True)
As with Django’s native implementation,
Nonecan be supplied in place oflookupsto negate a previous application ofprefetch_related(). By default, this will only work for cases whereprefetch_related()was called withoutfor_specific_subqueries, or withfor_specific_subqueries=False. However, you can usefor_specific_subqueries=Trueto negate subquery-specific applications too. For example:# Fetch 'contributors' and 'listing_image' renditions when retrieving specific page data queryset = Page.objects.specific().prefetch_related( "contributors", "listing_image__renditions", for_specific_subqueries=True ) # I've changed my mind. Do not make any additional queries queryset = queryset.prefetch_related(None, for_specific_subqueries=True)
#### 성능 고려 사항
prefetch_related() 의 일반적인 사용은 제공된 각 lookups 에 대해 추가 데이터베이스 쿼리가 실행됩니다. 그러나 for_specific_subqueries=True 와 결합하면 이 추가 데이터베이스 쿼리 수는 결과의 각 특정 유형에 대해 곱해집니다. 소수의 객체만 가져오거나 결과의 유형 편차가 높을 가능성이 있는 경우, 이러한 추가 쿼리를 만드는 추가 오버헤드가 실제로 성능에 부정적인 영향을 미칠 수 있습니다.
for_specific_subqueries=True 와 함께 prefetch_related() 를 사용하는 것은 많은 수의 결과가 필요하거나 유형 편차가 어떤 식으로든 제한되는 경우에만 사용해야 합니다. 예를 들어, 부모에 allow_subtypes 가 설정되어 결과가 소수의 페이지 유형으로 제한되는 자식 페이지 목록을 렌더링할 때. 또는 type() 또는 not_type() 필터가 쿼리셋을 소수의 특정 유형으로 제한하기 위해 적용된 경우.