Refs #31136 -- Made QuerySet.values()/values_list() group only by selected annotation.

Regression in 0f843fdd5b.
This commit is contained in:
Mariusz Felisiak 2020-01-15 09:32:42 +01:00 committed by GitHub
parent 63e6ee1f99
commit 59b4e99dd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 7 deletions

View file

@ -1165,6 +1165,31 @@ class AggregateTestCase(TestCase):
'Sams',
])
def test_aggregation_subquery_annotation_values(self):
"""
Subquery annotations and external aliases are excluded from the GROUP
BY if they are not selected.
"""
books_qs = Book.objects.annotate(
first_author_the_same_age=Subquery(
Author.objects.filter(
age=OuterRef('contact__friends__age'),
).order_by('age').values('id')[:1],
)
).filter(
publisher=self.p1,
first_author_the_same_age__isnull=False,
).annotate(
min_age=Min('contact__friends__age'),
).values('name', 'min_age').order_by('name')
self.assertEqual(list(books_qs), [
{'name': 'Practical Django Projects', 'min_age': 34},
{
'name': 'The Definitive Guide to Django: Web Development Done Right',
'min_age': 29,
},
])
@skipUnlessDBFeature('supports_subqueries_in_group_by')
def test_group_by_subquery_annotation(self):
"""