ratata
ratata

Reputation: 1169

Django wagtail getting page children with levels

I have a page structure as ' home_page > blog_index page > blog_page '. And a blog page can have sub pages. Now I am trying to create a blog archive menu.

Say I have a blog page named '1' and it has children 'a', 'b', 'c'. And blog page named '2' and it has children 'a', 'b', 'c'.

When we loop in BlogPages we have eight pages as '1', 'a', 'b', 'c', '2', 'a', 'b', 'c'.

Is there any chance that we get children with specific levels? For example if I want only '1' and '2' what can I do?

Thank you

Upvotes: 2

Views: 721

Answers (1)

gasman
gasman

Reputation: 25227

If you know the absolute depth of the pages you want within the page tree, you can write something like: BlogPage.objects.filter(depth=4). Here depth=1 is the root level; depth=2 would be a site homepage created at root level; depth=3 would be the blog index page, and depth=4 would be the blog pages that are direct children of the blog index.

This approach probably isn't very robust - it'll break if you reorganise your site so that the blog index is at a different level - so it might be better to say "fetch all BlogPages that are direct children of the BlogIndexPage" instead. You can do that as follows:

blog_index_page = BlogIndexPage.objects.first()
blog_pages = BlogPage.objects.child_of(blog_index_page)

Upvotes: 2

Related Questions