diff options
| author | Iacopo Spalletti <i.spalletti@nephila.it> | 2016-04-02 12:39:39 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-02 07:24:01 -0400 |
| commit | 9956d89fa2a5443e4bdba472c34b147a239429eb (patch) | |
| tree | 33abc64b2064c76f445fe86a86228b0a83b6fe71 /docs | |
| parent | 0325483e37dd83fc668daebc7a7dca6c365f51cc (diff) | |
[1.9.x] Fixed #22268 -- Documented values_list() behavior for multivalued relations.a
Thanks Sai Krishna for the initial patch.
Backport of 7d485d5d75bd9faab0b949fd34d4f098f8079452 from master
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 23f4c9c4c0..6ea5390dff 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -647,6 +647,30 @@ achieve that, use ``values_list()`` followed by a ``get()`` call:: >>> Entry.objects.values_list('headline', flat=True).get(pk=1) 'First entry' +``values()`` and ``values_list()`` are both intended as optimizations for a +specific use case: retrieving a subset of data without the overhead of creating +a model instance. This metaphor falls apart when dealing with many-to-many and +other multivalued relations (such as the one-to-many relation of a reverse +foreign key) because the the "one row, one object" assumption doesn't hold. + +For example, notice the behavior when querying across a +:class:`~django.db.models.ManyToManyField`:: + + >>> Author.objects.values_list('name', 'entry__headline') + [('Noam Chomsky', 'Impressions of Gaza'), + ('George Orwell', 'Why Socialists Do Not Believe in Fun'), + ('George Orwell', 'In Defence of English Cooking'), + ('Don Quixote', None)] + +Authors with multiple entries appear multiple times and authors without any +entries have ``None`` for the entry headline. + +Similarly, when querying a reverse foreign key, ``None`` appears for entries +not having any author:: + + >>> Entry.objects.values_list('authors') + [('Noam Chomsky',), ('George Orwell',), (None,)] + ``dates()`` ~~~~~~~~~~~ |
