Get last record in a queryset
Django Doc:
latest(field_name=None)
returns the latest object in the table, by date, using the field_name provided as the date field.This example returns the latest Entry in the table, according to the
pub_date
field:Entry.objects.latest('pub_date')
EDIT : You now have to use Entry.objects.latest('pub_date')
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).