[Django] #12608: Inconsistent results from values and values_list when using annotate

2 views
Skip to first unread message

Django

unread,
Jan 14, 2010, 5:08:21 AM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12608: Inconsistent results from values and values_list when using annotate
---------------------------+------------------------------------------------
Reporter: mattmcc | Owner: nobody
Status: new | Milestone: 1.2
Component: Uncategorized | Version: SVN
Keywords: | Stage: Unreviewed
Has_patch: 0 |
---------------------------+------------------------------------------------
To group users by email and get a count, one might do:
{{{
>>> User.objects.values('email').annotate(Count('email'))
[{'email__count': 2, 'email': u'us...@example.com'}]
}}}
However, values_list discards the annotated value:
{{{
>>> User.objects.values_list('email').annotate(Count('email'))
[(u'us...@example.com',)]
}}}

--
Ticket URL: <http://code.djangoproject.com/ticket/12608>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 14, 2010, 5:08:56 AM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#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: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by mattmcc):

* keywords: => values_list annotate
* needs_better_patch: => 0
* component: Uncategorized => Database layer (models, ORM)
* needs_tests: => 0
* needs_docs: => 0

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:1>

Django

unread,
Feb 9, 2010, 9:12:41 AM2/9/10
to djang...@holovaty.com, django-...@googlegroups.com
#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: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by russellm):

* stage: Unreviewed => Accepted

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:2>

Django

unread,
Feb 22, 2010, 4:32:46 PM2/22/10
to djang...@holovaty.com, django-...@googlegroups.com
#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'us...@example.com', 2)]

In [4]: User.objects.values('email').annotate(Count('email'))
Out[4]: [{'email__count': 2, 'email': u'us...@example.com'}]

In [5]: User.objects.values_list('email')
Out[5]: [(u'us...@example.com',), (u'us...@example.com',)]
}}}

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:3>

Django

unread,
Feb 22, 2010, 4:42:36 PM2/22/10
to djang...@holovaty.com, django-...@googlegroups.com
#12608: Inconsistent results from values and values_list when using annotate
---------------------------------------------------+------------------------
Reporter: mattmcc | Owner: coleifer
Status: assigned | 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):

* owner: nobody => coleifer
* status: new => assigned

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:4>

Django

unread,
Feb 22, 2010, 6:07:38 PM2/22/10
to djang...@holovaty.com, django-...@googlegroups.com
#12608: Inconsistent results from values and values_list when using annotate
---------------------------------------------------+------------------------
Reporter: mattmcc | Owner: coleifer
Status: assigned | Milestone: 1.2
Component: Database layer (models, ORM) | Version: SVN
Resolution: | Keywords: values_list annotate
Stage: Ready for checkin | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by Alex):

* stage: Accepted => Ready for checkin

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:5>

Django

unread,
Feb 23, 2010, 4:46:47 PM2/23/10
to djang...@holovaty.com, django-...@googlegroups.com
#12608: Inconsistent results from values and values_list when using annotate
---------------------------------------------------+------------------------
Reporter: mattmcc | Owner: coleifer
Status: closed | Milestone: 1.2
Component: Database layer (models, ORM) | Version: SVN
Resolution: fixed | Keywords: values_list annotate
Stage: Ready for checkin | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Comment (by coleifer):

Updated doctest to work with postgresql. Tested & working now.

--
Ticket URL: <http://code.djangoproject.com/ticket/12608#comment:7>
Reply all
Reply to author
Forward
0 new messages