On Fri, Apr 23, 2010 at 1:29 AM, ChrisR <robthech...@gmail.com> wrote: > def get_prev_by_title(self): > get_prev = Product.objects.order_by('- > title').filter(title__lt=self.title) > try: > return get_prev[0] > except IndexError: > return None > > > def get_next_by_title(self): > get_next = > Product.objects.order_by('title').filter(title__gt=self.title) > try: > return get_next[0] > except IndexError: > return None
just a little DRYer: ====== take 1 ======== def firstornone (q): try: return q[0] except IndexError: return None ............. def get_prev_by_title(self): return firstornone(Product.objects.order_by('-title').filter(title__lt=self.title)) def get_next_by_title(self): return firstornone(Product.objects.order_by('title').filter(title__gt=self.title)) ==================== or even: ======== take 2 ======= def next_by (queryset, field, value): return firstornone (queryset.order_by(field).filter(**{'%s__gt'%field:value})) def prev_by (queryset, field, value): return firstornone (queryset.order_by('-'+field).filter(**{'%s__lt'%field:value})) ..... def get_prev_by_title(self): return prev_by(Product.objects.all(), 'title', self.title) def get_next_by_title(self): return next_by(Product.objects.all(), 'title', self.title) ================= (a slow morning, i felt the itch to code a little....) -- Javier -- 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.