views:

192

answers:

3

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 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
thnx...didn't know this
Stephen
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
@jujule - I've edited that warning in to your answer, hope that's OK.
Dominic Rodger
A: 

Maybe you can use something like this:

queryset[queryset.count()-1]
fsouto
A: 

Django Doc

Entry.objects.latest('pub_date')
Asinox