开发者

Removing a result from Queryset

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 pag开发者_运维百科es 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.


You can simply slice the results :)

objects = your_pre_sliced_queryset[:pagesize]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜