I know how to do this with 3 queries: object = Model.objects.filter(pk=pk) previous = Model.objects.filter(votes__lt=object.votes).order_by('-votes')[:1][0] next = Model.objects.filter(votes__gt=object.votes).order_by('votes')[:1][0]
My only problem with this is if there is any object with the same value for votes. ______________________ Vinícius Mendes Solucione Sistemas http://solucione.info/ On Wed, Mar 31, 2010 at 7:45 PM, x13 <fcmira...@gmail.com> wrote: > Hello, > > I'm trying to figure out a simple way to get a known record from a > database but also get the "previous" and "next" records too. My model > is a Photo table with user's votes : > > ID Votes > > 1 24 > 2 5 > 3 102 > 4 21 > 5 10 > > > So, if I query for the photo with ID=4 (21 votes) I'd like to get the > next photo in the votes rank (ID=1 with 24 votes) and the previous > photo in the rank (ID=5 with 10 votes). Can I do this with only one > query to the database? Ideas? > > Thanks in advance. > Cheers! > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.