Skip to content

Instantly share code, notes, and snippets.

@h3
Created July 3, 2012 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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