通过parent_page_types限制HomePage仅作为root的直接子项可用

问题描述 投票:7回答:2

我在我的Page-models周围愉快地使用parent_page_typessubpage_types

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

任何提示?

wagtail
2个回答
13
投票

试试这个:

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()

1
投票

首先,对@Serafeim的回答竖起大拇指,但我会为寻找类似我的问题的人发布我的回答。

我希望在多站点模式下为特定父项实现相同的功能。意思是我想拥有多个站点“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
© www.soinside.com 2019 - 2024. All rights reserved.