1

我有两个模型,ParentPageChildPage。我想找到ParentPage一个字段在 s 上的is_completed集合。TrueChildPage

通常在 Django 中,我可以做类似的事情

ParentPage.objects.filter(child_page__is_completed=True)

但是,我认为 Wagtail/Treebeard 层次结构在这里没有连接。

我还认为您可以ChildPage通过多个ParentPages 过滤 s,例如ChildPage.objects.children_of([parent_ids]),但我也看不到这样做的方法。

有没有更简单的方法?

4

1 回答 1

1

Page 表有pathurl_path列。path如果您找到所有子页面并去掉or的最后一部分,则url_path可以使用该结果查询父页面。

小路:

child_paths = ChildPage.objects.filter(is_completed=True).values_list("path", flat=True)
parent_paths = set([cp[:-4] for cp in child_paths])
pages = Page.objects.filter(path__in=parent_paths)

网址路径:

child_urls = ChildPage.objects.filter(is_completed=True).values_list("url_path", flat=True)
parent_urls = set([url.rsplit('/', 1)[0] for url in child_urls])
pages = Page.objects.filter(url_path__in=parent_urls)

免责声明:未经测试的代码。

于 2020-10-10T22:48:31.000 回答