tags:

views:

26

answers:

1

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.

+2  A: 

You're looking for Posts.get_{next,previous}_by_FOO().

Ignacio Vazquez-Abrams
Perfect, thank you.
Vernon
I didn't get the method described to work, but it did help me search a little better. What I finally got working was:Posts.objects.order_by('published').filter(published__lt=FOO.published)[0]and substitute published__lt for published__gthttp://groups.google.com/group/django-users/browse_thread/thread/602d74da27cadf83
Vernon