Fixed #31990 -- Fixed QuerySet.ordered for GROUP BY queries on models with Meta.ordering.

Regression in 0ddb4ebf7b.

Thanks Julien Dutriaux for the report.
This commit is contained in:
Mariusz Felisiak 2020-09-14 20:07:44 +02:00 committed by GitHub
parent 5fab16392f
commit e11d05e0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -1224,7 +1224,12 @@ class QuerySet:
return True
if self.query.extra_order_by or self.query.order_by:
return True
elif self.query.default_ordering and self.query.get_meta().ordering:
elif (
self.query.default_ordering and
self.query.get_meta().ordering and
# A default ordering doesn't affect GROUP BY queries.
not self.query.group_by
):
return True
else:
return False

View File

@ -11,3 +11,8 @@ Bugfixes
* Fixed a bug in Django 3.1 where ``FileField`` instances with a callable
storage were not correctly deconstructed (:ticket:`31941`).
* Fixed a regression in Django 3.1 where the :attr:`.QuerySet.ordered`
attribute returned incorrectly ``True`` for ``GROUP BY`` queries (e.g.
``.annotate().values()``) on models with ``Meta.ordering``. A model's
``Meta.ordering`` doesn't affect such queries (:ticket:`31990`).

View File

@ -2084,6 +2084,16 @@ class QuerysetOrderedTests(unittest.TestCase):
self.assertIs(qs.ordered, False)
self.assertIs(qs.order_by('num_notes').ordered, True)
def test_annotated_default_ordering(self):
qs = Tag.objects.annotate(num_notes=Count('pk'))
self.assertIs(qs.ordered, False)
self.assertIs(qs.order_by('name').ordered, True)
def test_annotated_values_default_ordering(self):
qs = Tag.objects.values('name').annotate(num_notes=Count('pk'))
self.assertIs(qs.ordered, False)
self.assertIs(qs.order_by('name').ordered, True)
@skipUnlessDBFeature('allow_sliced_subqueries_with_in')
class SubqueryTests(TestCase):