Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ce5cb0

Browse files
michalnikcharettes
authored andcommitted
Fixed #26434 -- Removed faulty clearing of ordering field when missing from explicit grouping.
Co-authored-by: Simon Charette <charette.s@gmail.com>
1 parent0174a85 commit2ce5cb0

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

‎django/db/models/sql/query.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ def get_aggregation(self, using, aggregate_exprs):
534534
# Queries with distinct_fields need ordering and when a limit is
535535
# applied we must take the slice from the ordered query. Otherwise
536536
# no need for ordering.
537-
inner_query.clear_ordering(force=False)
537+
ifinner_query.orderby_issubset_groupby:
538+
inner_query.clear_ordering(force=False)
538539
ifnotinner_query.distinct:
539540
# If the inner query uses default select and it has some
540541
# aggregate annotations, then we must make sure the inner
@@ -2338,6 +2339,33 @@ def add_ordering(self, *ordering):
23382339
else:
23392340
self.default_ordering=False
23402341

2342+
@property
2343+
deforderby_issubset_groupby(self):
2344+
ifself.extra_order_by:
2345+
# Raw SQL from extra(order_by=...) can't be reliably compared
2346+
# against resolved OrderBy/Col expressions. Treat as not a subset.
2347+
returnFalse
2348+
ifself.group_byin (None,True):
2349+
# There is either no aggregation at all (None), or the group by
2350+
# is generated automatically from model fields (True), in which
2351+
# case the order by is necessarily a subset of them.
2352+
returnTrue
2353+
ifnotself.order_by:
2354+
# Although an empty set is always a subset, there's no point in
2355+
# clearing ordering when there isn't any. Avoid the clone() below.
2356+
returnTrue
2357+
# Don't pollute the original query (might disrupt joins).
2358+
q=self.clone()
2359+
order_by_set= {
2360+
(
2361+
order_by.resolve_expression(q)
2362+
ifhasattr(order_by,"resolve_expression")
2363+
elseF(order_by).resolve_expression(q)
2364+
)
2365+
fororder_byinq.order_by
2366+
}
2367+
returnorder_by_set.issubset(self.group_by)
2368+
23412369
defclear_ordering(self,force=False,clear_default=True):
23422370
"""
23432371
Remove any ordering settings if the current query allows it without

‎tests/aggregation_regress/tests.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ def assertObjectAttrs(self, obj, **kwargs):
171171
forattr,valueinkwargs.items():
172172
self.assertEqual(getattr(obj,attr),value)
173173

174+
deftest_count_preserve_group_by(self):
175+
# new release of the same book
176+
Book.objects.create(
177+
isbn="113235613",
178+
name=self.b4.name,
179+
pages=self.b4.pages,
180+
rating=4.0,
181+
price=Decimal("39.69"),
182+
contact=self.a5,
183+
publisher=self.p3,
184+
pubdate=datetime.date(2018,11,3),
185+
)
186+
qs=Book.objects.values("contact__name","publisher__name").annotate(
187+
publications=Count("id")
188+
)
189+
self.assertEqual(qs.order_by("id").count(),len(qs.order_by("id")))
190+
self.assertEqual(qs.extra(order_by=["id"]).count(),len(qs.order_by("id")))
191+
174192
deftest_annotation_with_value(self):
175193
values= (
176194
Book.objects.filter(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp