Get last record in a queryset

Stephen  picture Stephen · Feb 3, 2010 · Viewed 85.1k times · Source

How can I retrieve the last record in a certain queryset?

Answer

Asinox picture Asinox · Aug 23, 2010

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')