Django / Wagtail - 只有父页面的第一页
Django / Wagtail - Only first page of a parent
我正在尝试 return 树中最新子项的列表。 RecordPage
只能添加到特定节点,我想在主页上添加 "latest" 条目的列表,但 return 仅在每个父节点的 RecordPage
子节点上节点。
我试过这样的事情:
return RecordPage.objects.distinct("parent").live().order_by("-first_published_at")[:5]
但是 Django 对我说模型中不存在父字段感到难过,这是有道理的。我无法从文档中弄清楚这一点。
请帮忙?
wagtailcore_page
table没有这样的字段。了解父级的唯一方法是通过 path
或 url_path
但它会很难看。
但在我们尝试进入复杂的东西以使其仅在一个查询中工作之前,它可以在 n+1 个查询中完成。鉴于您说 RecordPage
只能添加到特定节点,我假设这些特定节点称为 RecordIndexPage
。这样,我们就可以查询所有我们感兴趣的RecordIndexPage
,并且对于它们中的每一个,查询最新的RecordPage
和descendant_of,就像这样:
record_pages = []
for index_page in RecordIndexPage.objects.all():
record_page = RecordPage.objects \
.descendant_of(index_page) \
.live() \
.order_by('-first_published_at') \
.first()
if record_page:
record_pages.append(record_page)
record_pages = sorted(record_pages, key=lambda page: page.first_published_at, reverse=True)
现在,如果我们想尝试在单个查询中执行此操作,我们可能可以使用 path
破解一些东西。我假设您现在所有的 RecordPage
都处于同一深度,因为这会使事情变得简单得多。
注意:这是一个比其他任何东西都更进一步探索的想法,我还没有尝试过这个代码。
from django.db.models.functions import Substr
# Path is a string of length `depth` * 4.
# e.g. `0001` is the path of a page at depth 1, `00010003` is the path of a page at depth 2, etc.
path_step = 4
# Given the root page is always at depth 1 and the homepage is a depth 2,
# the `RecordIndexPage`s would be at depth 3.
parent_depth = 3
# Querying.
record_pages = RecordPage.objects \
.annotate(parent_path=Substr('path', 0, parent_depth * path_step)) \
.live() \
.order_by('-first_published_at') \
.distinct('parent_path')
record_pages = record_pages[:5]
我正在尝试 return 树中最新子项的列表。 RecordPage
只能添加到特定节点,我想在主页上添加 "latest" 条目的列表,但 return 仅在每个父节点的 RecordPage
子节点上节点。
我试过这样的事情:
return RecordPage.objects.distinct("parent").live().order_by("-first_published_at")[:5]
但是 Django 对我说模型中不存在父字段感到难过,这是有道理的。我无法从文档中弄清楚这一点。
请帮忙?
wagtailcore_page
table没有这样的字段。了解父级的唯一方法是通过 path
或 url_path
但它会很难看。
但在我们尝试进入复杂的东西以使其仅在一个查询中工作之前,它可以在 n+1 个查询中完成。鉴于您说 RecordPage
只能添加到特定节点,我假设这些特定节点称为 RecordIndexPage
。这样,我们就可以查询所有我们感兴趣的RecordIndexPage
,并且对于它们中的每一个,查询最新的RecordPage
和descendant_of,就像这样:
record_pages = []
for index_page in RecordIndexPage.objects.all():
record_page = RecordPage.objects \
.descendant_of(index_page) \
.live() \
.order_by('-first_published_at') \
.first()
if record_page:
record_pages.append(record_page)
record_pages = sorted(record_pages, key=lambda page: page.first_published_at, reverse=True)
现在,如果我们想尝试在单个查询中执行此操作,我们可能可以使用 path
破解一些东西。我假设您现在所有的 RecordPage
都处于同一深度,因为这会使事情变得简单得多。
注意:这是一个比其他任何东西都更进一步探索的想法,我还没有尝试过这个代码。
from django.db.models.functions import Substr
# Path is a string of length `depth` * 4.
# e.g. `0001` is the path of a page at depth 1, `00010003` is the path of a page at depth 2, etc.
path_step = 4
# Given the root page is always at depth 1 and the homepage is a depth 2,
# the `RecordIndexPage`s would be at depth 3.
parent_depth = 3
# Querying.
record_pages = RecordPage.objects \
.annotate(parent_path=Substr('path', 0, parent_depth * path_step)) \
.live() \
.order_by('-first_published_at') \
.distinct('parent_path')
record_pages = record_pages[:5]