Wednesday, July 28, 2010

Re: implementing next and previous of the object

It's probably best to do this in a custom Manager.
This code is based on this answer: http://markmail.org/message/kwwuskco4gilej2w

You get the idea

class GetPrevNextManager(models.Manager):
def get_next_by_id(self, object):
qs = self.filter(id__gt=object.id)
if qs.count() > 0:
return qs.order_by('id')[0]
# empty queryset
return qs

def get_prev_by_id(self, object):
qs = self.filter(id__lt=object.d)
if qs.count() > 0:
return qs.order_by('-id')[0]
# empty queryset
return qs

More information about managers: http://docs.djangoproject.com/en/dev/topics/db/managers/

- Sævar

On Jul 28, 6:41 am, haibin <cai.hai...@gmail.com> wrote:
> Hi all,
>
> Need help, advice on how the have a next and previous link on a object
> detail page. For example fomr a object list page a user chose an
> object, and in the object page instead of going back to the list to
> view other object the user just click either next or previous.
>
> Any idea how to implement it?
>
> Thanks,
> James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment