How can I retrieve the last record in a certain queryset?
+7
A:
You could simply do something like this, using reverse()
:
queryset.reverse()[0]
Also, beware this warning from the Django documentation:
... note that
reverse()
should generally only be called on a QuerySet which has a defined ordering (e.g., when querying against a model which defines a default ordering, or when usingorder_by()
). If no such ordering is defined for a givenQuerySet
, callingreverse()
on it has no real effect (the ordering was undefined prior to callingreverse()
, and will remain undefined afterward).
jujule
2010-02-03 09:57:29
thnx...didn't know this
Stephen
2010-02-03 09:59:21
as said in django docs, : "note that reverse() should generally only be called on a QuerySet which has a defined ordering (e.g., when querying against a model which defines a default ordering, or when using order_by()). If no such ordering is defined for a given QuerySet, calling reverse() on it has no real effect (the ordering was undefined prior to calling reverse(), and will remain undefined afterward)."
jujule
2010-02-03 11:09:37
@jujule - I've edited that warning in to your answer, hope that's OK.
Dominic Rodger
2010-02-03 11:29:52