Getting an entry before and after a given entry in a Django Queryset
Posted
by Vernon
on Stack Overflow
See other posts from Stack Overflow
or by Vernon
Published on 2010-04-23T06:35:57Z
Indexed on
2010/04/23
6:43 UTC
Read the original article
Hit count: 302
I am creating a simple blog as part of a website and I am getting stuck on something that I am assuming is simple.
If I call any blog post, say by it's title, from a queryset, how can I get the entry before and after the post in it's published order.
I can iterate over the whole thing, get the position of the entry I have and use that to call the one before and the one after. But that is a long bit of code for something that I am sure I can do more simply.
What I want would be something like this:
next_post = Posts.object.filter(title=current_title).order_by("-published")[-1]
Of course because of the filter, it is not going to work, but just to give you the idea of what I am looking for.
© Stack Overflow or respective owner