Change: Retrieving the Last Object

created on Nov. 13, 2012, 12:44 a.m. by Hevok & updated on Nov. 13, 2012, 12:44 a.m. by Hevok

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]

Tags: query, django
Categories: Tutorial
Parent: Web Framework

Comment: Created entry.

See entry | Admin

Comment on This Data Unit