Last active
October 15, 2016 19:56
-
-
Save kyle-eshares/0c30d81c4f8f201a41b916a2dd864b2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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