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

时间:2016-05-11 16:02:42

标签: wagtail

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

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

任何提示?

2 个答案:

答案 0 :(得分:12)

试试这个:

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 :(得分: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