Wagtail: get the previous or next brother

I create a page with a shiver where I need to know the previous and next sister of the current page:

In my portrait page model, I tried to define two methods to find the correct URLs, but I missed the important part. To get the first brother, I can simply do the following:

class PortraitPage(Page):

    ...

    def first_portrait(self):
        return self.get_siblings().live().first().url

There is a method first()and last(), but it seems, the method is next()either previous()not used to get direct neighbors (in the order in which they are located in the wagtail administrator).

Is there any way to achieve this?

+4
source share
3 answers

, , : get_prev_sibling() get_next_sibling().

, ( ):

def prev_portrait(self):
    if self.get_prev_sibling():
        return self.get_prev_sibling().url
    else:
        return self.get_siblings().last().url

def next_portrait(self):
    if self.get_next_sibling():
        return self.get_next_sibling().url
    else:
        return self.get_siblings().first().url
+1

Django-Treebeard get_next_sibling get_prev_sibling, , . , :

prev = page.get_prev_siblings().live().first()
next = page.get_next_siblings().live().first()

, , .

+6

Here is the version used to handle non-publishedsiblings.

def next_portrait(self):
    next_sibling = self.get_next_sibling()
    if next_sibling and next_sibling.live:
        return next_sibling.url
    else:
        next_published_siblings = self.get_next_siblings(
            inclusive=False
        ).live()
        if len(next_published_siblings):
            return next_published_siblings[0].url
        return self.get_siblings().live().first().url

def prev_portrait(self):
    previous_sibling = self.get_prev_sibling()
    if previous_sibling and previous_sibling.live:
        return previous_sibling.url
    else:
        previous_published_siblings = self.get_prev_siblings(
            inclusive=False
        ).live()
        if len(previous_published_siblings):
            return previous_published_siblings[0].url
        return self.get_siblings().live().last().url
+1
source

Source: https://habr.com/ru/post/1588954/


All Articles