Fixed #26434 -- Removed faulty clearing of ordering field when missing from explicit grouping.
Some checks failed
Docs / spelling (push) Has been cancelled
Docs / blacken-docs (push) Has been cancelled
Docs / lint-docs (push) Has been cancelled
Linters / isort (push) Waiting to run
Linters / black (push) Waiting to run
Linters / flake8 (push) Waiting to run
Tests / Windows, SQLite, Python 3.14 (push) Waiting to run
Tests / JavaScript tests (push) Waiting to run

Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
Michal Mládek 2025-05-26 18:37:34 +02:00 committed by Jacob Walls
parent 0ea01101c3
commit ea3a71c2d0
2 changed files with 26 additions and 1 deletions

View file

@ -2346,7 +2346,13 @@ class Query(BaseExpression):
query (not even the model's default).
"""
if not force and (
self.is_sliced or self.distinct_fields or self.select_for_update
self.is_sliced
or self.distinct_fields
or self.select_for_update
or (
isinstance(self.group_by, tuple)
and not {*self.order_by, *self.extra_order_by}.issubset(self.group_by)
)
):
return
self.order_by = ()

View file

@ -171,6 +171,25 @@ class AggregationTests(TestCase):
for attr, value in kwargs.items():
self.assertEqual(getattr(obj, attr), value)
def test_count_preserve_group_by(self):
# new release of the same book
Book.objects.create(
isbn="113235613",
name=self.b4.name,
pages=self.b4.pages,
rating=4.0,
price=Decimal("39.69"),
contact=self.a5,
publisher=self.p3,
pubdate=datetime.date(2018, 11, 3),
)
qs = Book.objects.values("contact__name", "publisher__name").annotate(
publications=Count("id")
)
self.assertEqual(qs.count(), Book.objects.count() - 1)
self.assertEqual(qs.order_by("id").count(), Book.objects.count())
self.assertEqual(qs.extra(order_by=["id"]).count(), Book.objects.count())
def test_annotation_with_value(self):
values = (
Book.objects.filter(