views:

32

answers:

1

Is there a simple way to discard/remove the last result in a queryset without affecting the db?

I am trying to paginate results in Django, but don't know the total number of objects for a given query.

I was planning on using next/previous or older/newer links, so I only need to know if this is the first and/or last page.

First is easy to check. To check for the last page I can compare the number of results with the pagesize or make a second query. The first method fails to detect the last page when the number of results in the last set equals the pagesize (ie 100 records get broken into 10 pages with the last page containing exactly 10 results) and I would like to avoid making a second query.

My current thought is that I should fetch pagesize + 1 results from the db. If the queryset length equals 11, I know this is not the last page and I want to discard the last result in the queryset before passing the queryset to the template.

A: 

You can simply slice the results :)

objects = your_pre_sliced_queryset[:pagesize]

WoLpH