9

我很高兴地使用parent_page_typessubpage_types的页面模型。

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

有什么提示吗?

4

2 回答 2

24

尝试这个:

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()
于 2016-05-11T16:14:21.540 回答
2

首先,为@Serafeim 的答案竖起大拇指,但我会将我的答案发布给搜索与我类似的问题的人。

我想实现相同的目标,但对于多站点模式下的特定父级。这意味着我想拥有多个站点“主页”,但每个“主页”只能包含一个“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
于 2019-04-15T18:11:42.820 回答