Skip to content

Instantly share code, notes, and snippets.

@h3
Created July 3, 2012 03:58
Show Gist options
  • Save h3/3037571 to your computer and use it in GitHub Desktop.
Save h3/3037571 to your computer and use it in GitHub Desktop.
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