commit 0e503e3aa1107dcedca7be20d919bf4f2712d8bd
Author: Michal Mládek <osvc.04923031@gmail.com>
Date: Wed May 21 07:38:55 2025 +0200
Related to #26434 - main logic
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 92a09c5840..89cfeb09fd 100644
|
a
|
b
|
class Query(BaseExpression):
|
| 2303 | 2303 | else: |
| 2304 | 2304 | self.default_ordering = False |
| 2305 | 2305 | |
| | 2306 | def order_needs_grouping(self, order_by): |
| | 2307 | """It tells if order by could be removed or not |
| | 2308 | #26434 - QuerySet.count() has too aggresive strategy to remove it |
| | 2309 | """ |
| | 2310 | return True |
| | 2311 | |
| 2306 | 2312 | def clear_ordering(self, force=False, clear_default=True): |
| 2307 | 2313 | """ |
| 2308 | 2314 | Remove any ordering settings if the current query allows it without |
| … |
… |
class Query(BaseExpression):
|
| 2314 | 2320 | self.is_sliced or self.distinct_fields or self.select_for_update |
| 2315 | 2321 | ): |
| 2316 | 2322 | return |
| 2317 | | self.order_by = () |
| | 2323 | order_by = [] |
| | 2324 | if not force: |
| | 2325 | for item in self.order_by: |
| | 2326 | if not self.order_needs_grouping(item): |
| | 2327 | continue |
| | 2328 | order_by.append(item) |
| | 2329 | self.order_by = tuple(order_by) |
| 2318 | 2330 | self.extra_order_by = () |
| 2319 | 2331 | if clear_default: |
| 2320 | 2332 | self.default_ordering = False |