Django Wagtail Page.objects.descendant_of(inclusive=False) 错误

Django Wagtail Page.objects.descendant_of(inclusive=False) Error

我正在使用 Django 2.0 和最新版本的 Wagtail CMS。 我是 Wagtail 的新手,我正在尝试查询 EventPage 的 EventIndexPage 的子项,并让它们出现在我的主页上。

我正在尝试在下面的家庭应用 models.py 中使用 python 的这一行:

special_events = EventIndexPage.objects.descendant_of(inclusive=False)

但是它returns:

 return getattr(self.get_queryset(), name)(*args, **kwargs)
TypeError: descendant_of() missing 1 required positional argument: 'other'

当我尝试将 "EventPage" 放在 "inclusive=false" 之前时,出现类型错误,因为它是数组而不是整数。

文档:http://docs.wagtail.io/en/v2.0/reference/pages/queryset_reference.html

家庭应用的

Models.py:

from django.db import models
from event.models import EventIndexPage

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel


class HomePage(Page):
    body = RichTextField(blank=True)
    special_events = EventIndexPage.objects.descendant_of(inclusive=False)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]

Models.py 事件应用

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.search import index

class EventIndexPage(Page):
    intro = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('intro', classname="full")
    ]


class EventPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)

    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        index.SearchField('body'),
    ]

    content_panels = Page.content_panels + [
        FieldPanel('date'),
        FieldPanel('intro'),
        FieldPanel('body', classname="full"),
    ]

首先,这行代码放错了地方——直接放在 class HomePage(Page): 中意味着您的查询将在服务器启动时 运行 一次,结果将成为HomePage 的定义。您可能想让它成为 HomePage 的方法,这样您就可以在每次加载页面时重新 运行 查询:

class HomePage(Page):
    body = RichTextField(blank=True)

    def special_events(self):
        return EventIndexPage.objects.descendant_of...

现在让我们看看 descendant_of 在做什么。 EventIndexPage.objects.descendant_of(other_page) 表示 "find all of the EventIndexPage pages which are descendants (i.e. immediate children, or grandchildren, and so on) of other_page"。由于您实际上是在寻找 EventPage 页,而不是 EventIndexPage,因此您应该使用 EventPage.objects.descendant_of.

现在,我们需要提供 other_page - 即告诉它 他们需要成为哪个 页面的后代。您可以尝试 EventPage.objects.descendant_of(EventIndexPage),但这不会起作用,因为 EventIndexPage 是一个 类型 的页面 - 您的站点上可能有多个 EventIndexPage , 并且 descendant_of 需要被赋予一个特定的页面实例。如果你确定你的网站上只会有一个 EventIndexPage,你可以使用 EventIndexPage.objects.first() 来告诉它 "use the first EventIndexPage that you encounter",这使得最终函数:

class HomePage(Page):
    body = RichTextField(blank=True)

    def special_events(self):
        event_index_page = EventIndexPage.objects.first()
        return EventPage.objects.descendant_of(event_index_page)

您可以安全地忽略此处的 inclusive 选项:它只确定一个页面是否被认为是其自身的后代,即 descendant_of(other_page) 的结果是否可以包括 other_page .在这种情况下,EventPage.objects.descendant_of(event_index_page) 结果永远不会包含 event_index_page,因为 event_index_page 不是 EventPage

最后一个建议:您真的需要处理 EventIndexPage 吗?大概您不打算让 EventPage 存在于 EventIndexPage 之外,在这种情况下,您的查询是 "give me all EventPages that are descendants of EventIndexPage" 还是 "give me all EventPages regardless of where they exist" 并不重要。如果是这样,函数可以简单地变成:

class HomePage(Page):
    body = RichTextField(blank=True)

    def special_events(self):
        return EventPage.objects.all()

或者,如果您应用此条件是因为您打算拥有多个子站点,每个子站点都有自己的 EventPageIndex 和事件集,并且只想 return 当前的事件子站点,您可以利用以下事实,即所讨论的事件是 也是 当前主页的后代(即 self)以及 EventPageIndex:

class HomePage(Page):
    body = RichTextField(blank=True)

    def special_events(self):
        return EventPage.objects.descendant_of(self)