Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wagtail

I use parent_page_types and subpage_types happily all around my Page-models.

But I'm stuck at allowing my class HomePage(Page) only as a direct child at root level.

Any hints?

like image 863
tombreit Avatar asked May 11 '16 16:05

tombreit


2 Answers

Try this:

parent_page_types = ['wagtailcore.Page']

also, for completeness, to allow only one instance of a Homepage, add this classmethod to your 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()
like image 87
Serafeim Avatar answered Nov 11 '22 20:11

Serafeim


First of all, thumbs up for @Serafeim answer, but I will post my answer for people searching for issue similar to mine.

I wanted to achieve the same thing but for a specifi parent in multi-site mode. Meaning I wanted to have multiple site "HomePage" but each "HomePage" can only include one "SearchIndexPage". So the above answer would be modified to

    @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
like image 29
Ibrahim Awad Avatar answered Nov 11 '22 19:11

Ibrahim Awad