self.get_siblings() falls over if you want to do any filtering based on properties only found in the subclass since the PageQuerySet results are of type Page.
For me, I have a blog index page that can be filtered by category or tag.
The bog detail page has cards for the next and previous blog post at the end.
I wanted those prev/next posts to be according to the filter I landed on that page from.
To get around this, you need to query the objects belonging to the subclass (eg BlogDetailPage), filter those, then get the prev/next post using class.objects.sibling_of(self):
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
siblings = self.__class__.objects.sibling_of(self).live()
category_filter = request.GET.get("category", None)
tag_filter = request.GET.get("tag", None)
if category_filter:
siblings = siblings.filter(categories__slug__in=category_filter.split(","))
context["filter"] = '?category=' + category_filter
elif tag_filter:
siblings = siblings.filter(tags__slug__in=tag_filter.split(','))
context["filter"] = '?tag=' + tag_filter
else:
context["filter"] = ''
context["next_post"] = siblings.filter(path__gt=self.path).first()
context["previous_post"] = siblings.filter(path__lt=self.path).first()
return context
I used self.__class__.objects.sibling_of(self)
as this is in a super class with sub classed blog pages.
dir(self)
and looked through the available methods. And it works. Which version of wagtail do you have? I'm working on v.1.0b1 – Howerton