[3.0.x] Refs #31136 -- Made QuerySet.values()/values_list() group only by selected annotation.
Regression in0f843fdd5b
. Backport of59b4e99dd0
from master
This commit is contained in:
parent
6aac9f6b11
commit
a24686987f
|
@ -2098,13 +2098,6 @@ class Query(BaseExpression):
|
|||
self.clear_deferred_loading()
|
||||
self.clear_select_fields()
|
||||
|
||||
if self.group_by is True:
|
||||
self.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
|
||||
# Disable GROUP BY aliases to avoid orphaning references to the
|
||||
# SELECT clause which is about to be cleared.
|
||||
self.set_group_by(allow_aliases=False)
|
||||
self.clear_select_fields()
|
||||
|
||||
if fields:
|
||||
field_names = []
|
||||
extra_names = []
|
||||
|
@ -2126,6 +2119,14 @@ class Query(BaseExpression):
|
|||
self.set_annotation_mask(annotation_names)
|
||||
else:
|
||||
field_names = [f.attname for f in self.model._meta.concrete_fields]
|
||||
# Selected annotations must be known before setting the GROUP BY
|
||||
# clause.
|
||||
if self.group_by is True:
|
||||
self.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
|
||||
# Disable GROUP BY aliases to avoid orphaning references to the
|
||||
# SELECT clause which is about to be cleared.
|
||||
self.set_group_by(allow_aliases=False)
|
||||
self.clear_select_fields()
|
||||
|
||||
self.values_select = tuple(field_names)
|
||||
self.add_fields(field_names, True)
|
||||
|
|
|
@ -1163,6 +1163,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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue