Skip to content

Instantly share code, notes, and snippets.

@kyle-eshares
Last active October 15, 2016 19:56
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 kyle-eshares/0c30d81c4f8f201a41b916a2dd864b2c to your computer and use it in GitHub Desktop.
Save kyle-eshares/0c30d81c4f8f201a41b916a2dd864b2c to your computer and use it in GitHub Desktop.
# Retrieve values as a dictionary
>>> Book.objects.values('title', 'author__name')
<QuerySet [{'author__name': u'Nikolai Gogol', 'title': u'The Overcoat'}, {'author__name': u'Leo Tolstoy', 'title': u'War and Peace'}]>
# Retrieve values as a tuple
>>> Book.objects.values_list('title', 'author__name')
<QuerySet [(u'The Overcoat', u'Nikolai Gogol'),
(u'War and Peace', u'Leo Tolstoy')]>
>>> Book.objects.values_list('title')
<QuerySet [(u'The Overcoat',), (u'War and Peace',)]>
# With one value, it is easier to flatten the list
>>> Book.objects.values_list('title', flat=True)
<QuerySet [u'The Overcoat', u'War and Peace']>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment