2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-06-04 19:52:48 +02:00
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
from typing import TypeVar, List
|
|
|
|
|
|
|
|
T = TypeVar('T')
|
|
|
|
|
2012-09-29 01:33:15 +02:00
|
|
|
def last_n(n, query_set):
|
2016-06-04 19:52:48 +02:00
|
|
|
# type: (int, QuerySet[T]) -> List[T]
|
2012-09-29 01:33:15 +02:00
|
|
|
"""Get the last n results from a Django QuerySet, in a semi-efficient way.
|
|
|
|
Returns a list."""
|
|
|
|
|
|
|
|
# We don't use reversed() because we would get a generator,
|
|
|
|
# which causes bool(last_n(...)) to be True always.
|
|
|
|
|
|
|
|
xs = list(query_set.reverse()[:n])
|
|
|
|
xs.reverse()
|
|
|
|
return xs
|