From f3c956214366d590c0a588aaad83f8f770c78b76 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Fri, 4 Aug 2017 15:28:39 +0500 Subject: Fixed #15648 -- Allowed QuerySet.values_list() to return a namedtuple. --- docs/ref/models/querysets.txt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'docs/ref') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 5973f2b5a3..6d66b05957 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -626,7 +626,7 @@ You can also refer to fields on related models with reverse relations through ``values_list()`` ~~~~~~~~~~~~~~~~~ -.. method:: values_list(*fields, flat=False) +.. method:: values_list(*fields, flat=False, named=False) This is similar to ``values()`` except that instead of returning dictionaries, it returns tuples when iterated over. Each tuple contains the value from the @@ -651,6 +651,15 @@ rather than one-tuples. An example should make the difference clearer:: It is an error to pass in ``flat`` when there is more than one field. +You can pass ``named=True`` to get results as a +:func:`~python:collections.namedtuple`:: + + >>> Entry.objects.values_list('id', 'headline', named=True) + + +Using a named tuple may make use of the results more readable, at the expense +of a small performance penalty for transforming the results into a named tuple. + If you don't pass any values to ``values_list()``, it will return all the fields in the model, in the order they were declared. @@ -688,6 +697,10 @@ not having any author:: Support for expressions in ``*fields`` was added. +.. versionchanged:: 2.0 + + The ``named`` parameter was added. + ``dates()`` ~~~~~~~~~~~ -- cgit v1.3