Wagtail CMS:查找与当前页面相关的所有页面

Wagtail CMS: Finding all pages that have a relation to the current page

我试图了解如何通过 modelcluster ParentalKey 获取指向我站点中所有页面的链接,这些页面将当前查看的页面列为相关页面。

基本设置如下:

# RelatedLink inherits from LinkFields, 
# both directly copied from the wagtaildemo project

class ParentPageRelatedLink(Orderable, RelatedLink):
    page = ParentalKey('ParentPage', related_name='related_links')


class ParentPage(Page):
    parent_page_types = ['ParentPage']
    subpage_types = ['ParentPage', 'ChildPage']

    def child_pages(self):
        children = ChildPage.objects.live().descendant_of(self)
        return children

ParentPage.content_panels = [
    FieldPanel('title', classname="full title"),
    FieldPanel('body', classname="full"),
    InlinePanel(ParentPage, 'related_links', label="Related links"),
]


class ChildPage(Page):
    parent_page_types = ['ParentPage']
    parent_page_types = ['ChildPage']

    def parent_index(self):
        return self.get_ancestors().type(ParentPage).last()

ChildPage.content_panels = [
    FieldPanel('title', classname="full title"),
    FieldPanel('body', classname="full"),
    InlinePanel(ChildPage, 'related_links', label="Related links"),
]

如果理解正确,为了让每个 ParentPage 在其 related_links 中包含当前的 ChildPage,我必须遍历 ChildPage.parent_page_types 中列出的每个页面,测试当前的 ChildPage 是否是在 ParentPage.related_links 中,然后从每个 ParentPages 中输出我需要的任何内容。

如果 parent_page_types.

中列出的页面类型有很多实例,那么对数据库的查询似乎会很多

有没有更好的方法?

例如,modelcluster 是否通过在 ParentPageRelatedLink 中创建的 ParentalKey 启用任何类型的反向引用(例如 Flask-SQLAlchemy 在使用 db.relashionship(backref="something") 时提供的反向引用)?从检查数据库表看起来不像。

编辑

好的,看来 LinkFields 中的 related_name 可能是一种方法,但由于 LinkFields 被许多不同的继承,我无法将其设置为 "related_from" 之类的东西ParentPage-like 类,似乎我必须有单独的 LinkField 类,每个 ParentPage 都有自己独特的 ForeignKey(related_name="something") 定义...或 do as instructed in the django docs。 但是我最初想到的循环可能会更好?

class LinkFields(models.Model):
    link_external = models.URLField("External link", blank=True)
    link_page = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        related_name='+'
    )
    link_document = models.ForeignKey(
        'wagtaildocs.Document',
        null=True,
        blank=True,
        related_name='+'
    )

    @property
    def link(self):
        if self.link_page:
            return self.link_page.url
        elif self.link_document:
            return self.link_document.url
        else:
            return self.link_external

    panels = [
        FieldPanel('link_external'),
        PageChooserPanel('link_page'),
        DocumentChooserPanel('link_document'),
    ]

    class Meta:
        abstract = True

您应该能够在 ParentPage class 中定义 @property。类似于:

class ParentPage(Page):
    ...
    @property
    def related_pages(self):
        pages = ParentPageRelatedLink.objects.filter(page_id=self.id)
        return pages

试试这个:

parent_pages = ParentPage.objects.filter(related_links__linked_page=child_page)

"related_links" 取自要查询的模型上 ParentalKey 的 related_name 属性。 "linked_page" 是您要过滤的 ParentPageRelatedLink 模型上的一个字段。

https://docs.djangoproject.com/en/1.8/topics/db/queries/#lookups-that-span-relationships

parent_page_types与查询页面无关。它用于向可以在哪些页面类型下创建特定页面类型添加约束(例如,您可以说博客条目只能在博客中创建)。它与页面的查询方式无关