1
0
mirror of https://github.com/django/django.git synced 2025-10-27 15:46:10 +00:00

[1.8.x] Fixed #24171 -- Fixed failure with complex aggregate query and expressions

The query used a construct of qs.annotate().values().aggregate() where
the first annotate used an F-object reference and the values() and
aggregate() calls referenced that F-object.

Also made sure the inner query's select clause is as simple as possible,
and made sure .values().distinct().aggreate() works correctly.

Backport of fb146193c4 from master
This commit is contained in:
Anssi Kääriäinen
2015-03-04 14:56:20 +02:00
committed by Tim Graham
parent 83269b2935
commit 3a0fe942dd
4 changed files with 48 additions and 9 deletions

View File

@@ -7,7 +7,9 @@ from operator import attrgetter
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldError
from django.db.models import F, Q, Avg, Count, Max, StdDev, Sum, Variance
from django.db.models import (
F, Q, Avg, Count, Max, StdDev, Sum, Value, Variance,
)
from django.test import TestCase, skipUnlessDBFeature
from django.test.utils import Approximate
from django.utils import six
@@ -1129,6 +1131,14 @@ class AggregationTests(TestCase):
'select__avg': Approximate(1.666, places=2),
})
def test_annotate_distinct_aggregate(self):
# There are three books with rating of 4.0 and two of the books have
# the same price. Hence, the distinct removes one rating of 4.0
# from the results.
vals1 = Book.objects.values('rating', 'price').distinct().aggregate(result=Sum('rating'))
vals2 = Book.objects.aggregate(result=Sum('rating') - Value(4.0))
self.assertEqual(vals1, vals2)
class JoinPromotionTests(TestCase):
def test_ticket_21150(self):