Retrieving the Last Object

To retrieve the very last object from a table:

entry = Entry.objects.filter(published=True).order_by('-id')[0]

In raw mysql the optimal synthax would be::

SELECT * FROM table WHERE published=TRUE ORDER BY Z DESC LIMIT 1

This would directly translate into::

entry = Entry.objects.filter(published=True).order_by('-id')[:1][0]

Edit tutorial

Comment on This Data Unit