mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
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.
This commit is contained in:
committed by
Tim Graham
parent
63f2dd4ad7
commit
fb146193c4
@@ -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
|
||||
@@ -1232,6 +1234,14 @@ class AggregationTests(TestCase):
|
||||
)
|
||||
self.assertEqual(qs['publisher_awards'], 30)
|
||||
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user