通过 parent_page_types 限制主页仅可作为 root 的直接子级使用

Limit HomePage via parent_page_types to be only available as direct child of root

我在我的页面模型周围愉快地使用 parent_page_typessubpage_types

但我坚持只允许我的 class HomePage(Page) 作为根级别的直接子项。

有什么提示吗?

试试这个:

parent_page_types = ['wagtailcore.Page']

此外,为了完整起见,只允许一个主页实例,将这个类方法添加到您的 HomePage:

@classmethod
def can_create_at(cls, parent):
    # You can only create one of these!
    return super(HomePage, cls).can_create_at(parent) \
        and not cls.objects.exists()

首先,为@Serafeim 的回答点赞,但我会 post 我的回答是为了那些搜索与我类似的问题的人。

我想实现相同的目标,但要针对多站点模式下的特定父级。意思是我想要多个站点 "HomePage",但每个 "HomePage" 只能包含一个 "SearchIndexPage"。所以上面的答案会修改为

    @classmethod
    def can_create_at(cls, parent):
        # You can only create one of these!
        return super(SearchIndexPage, cls).can_create_at(parent) \
               and parent.get_children().type(SearchIndexPage).count() == 0