#12608: Inconsistent results from values and values_list when using annotate
---------------------------------------------------+------------------------
Reporter: mattmcc | Owner: nobody
Status: new | Milestone: 1.2
Component: Database layer (models, ORM) | Version: SVN
Resolution: | Keywords:
values_list annotate
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by coleifer):
* has_patch: 0 => 1
Comment:
I patched the ValuesList iterator to include aggregates in the fields if
specified:
{{{
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 8cb3dbe..31b3333 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -954,7 +954,8 @@ class ValuesListQuerySet(ValuesQuerySet):
# If a field list has been specified, use it. Otherwise, use
the
# full list of fields, including extras and aggregates.
if self._fields:
- fields = self._fields
+ fields = list(self._fields) + filter(lambda f: f not in
self._fields,
+ aggregate_names)
else:
fields = names
diff --git a/tests/modeltests/aggregation/models.py
b/tests/modeltests/aggregation/models.py
index 9ed638e..0e8d881 100644
--- a/tests/modeltests/aggregation/models.py
+++ b/tests/modeltests/aggregation/models.py
@@ -362,4 +362,7 @@ True
>>>
Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values_list('mean_age',
flat=True)
[34.5]
+>>>
Book.objects.values_list('price').annotate(count=Count('price')).order_by('-count',
'price')
+[(Decimal('29.69'), 2), (Decimal('23.09'), 1), (Decimal('30'), 1),
(Decimal('75'), 1), (Decimal('82.8'), 1)]
+
"""}
}}}
{{{
In [1]: from django.contrib.auth.models import User
In [2]: from django.db.models import Count
In [3]: User.objects.values_list('email').annotate(Count('email'))
Out[3]: [(u'[email protected]', 2)]
In [4]: User.objects.values('email').annotate(Count('email'))
Out[4]: [{'email__count': 2, 'email': u'[email protected]'}]
In [5]: User.objects.values_list('email')
Out[5]: [(u'[email protected]',), (u'[email protected]',)]
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:3>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.