0

I have a Wagtail site and I have a parent/child structure between a couple of page types:

class FrontPage(Page):
    subpage_types = ["InnerPage"]

    def get_next_page(self):
        return self.get_children().live().first()

class InnerPage(Page):
    parent_page_types = ["FrontPage"]

    def get_next_page(self):
        return self.get_next_siblings().live().first()

The Admin user can drag the InnerPages into their chosen order in the Wagtail Admin, which is great. FrontPage.get_children() then fetches the inner pages in that order, and the get_next_page() methods return the next pages in the same order.

I'd like to write tests to check that the get_next_page() methods do return the correct page. But I can't work out how to assign an order to pages when creating them for tests. I can see that when doing the SQL queries Wagtail orders by wagtailcore_page.path, but I'm not sure how to affect this when programmatically creating InnerPage objects.

Phil Gyford
  • 13,432
  • 14
  • 81
  • 143

1 Answers1

1

Assuming you're using parent_page.add_child(instance=new_page) when creating pages in your test - the add_child method will create the new page as the last child in the ordering, so you just need to make sure to create them in the order you want them to appear in.

For more control over where new pages are inserted in the ordering, you can use the add_sibling method instead:

parent_page = FrontPage.objects.first()
last_child = InnerPage(title='last child')
parent_page.add_child(instance=last_child
first_child = InnerPage(title='first child')
# insert first_child immediately before last_child
last_child.add_sibling(pos='left', instance=first_child)
gasman
  • 23,691
  • 1
  • 38
  • 56
  • That's exactly what I'm doing and it hadn't occurred to me that this was also setting an order! I was thinking of adding them in a "wrong" order, like your example, partly to ensure they weren't actually being ordered by publish time instead, without me knowing. Thank you. – Phil Gyford Feb 17 '22 at 13:46