Skip to content

Instantly share code, notes, and snippets.

@h3

h3/gist:3037571

Created Jul 3, 2012
Embed
What would you like to do?
Get first element of a QuerySet
def getLastDateModification(self):
qs = self.themefile_set.order_by('-date_modified')
# NEVER !!
return qs[0].date_modified
# The most efficient/failsafe way (this will add LIMIT 1 to the query):
rs = list(qs[:1])
return rs and rs[0].date_modified or None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment