mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #15648 -- Allowed QuerySet.values_list() to return a namedtuple.
This commit is contained in:
parent
a027447f56
commit
f3c9562143
4 changed files with 101 additions and 5 deletions
|
@ -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)
|
||||
<QuerySet [Row(id=1, headline='First entry'), ...]>
|
||||
|
||||
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()``
|
||||
~~~~~~~~~~~
|
||||
|
||||
|
|
|
@ -291,6 +291,9 @@ Models
|
|||
* Added support for expressions in :attr:`Meta.ordering
|
||||
<django.db.models.Options.ordering>`.
|
||||
|
||||
* The new ``named`` parameter of :meth:`.QuerySet.values_list` allows fetching
|
||||
results as named tuples.
|
||||
|
||||
Pagination
|
||||
~~~~~~~~~~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue