summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/query.py2
-rw-r--r--tests/distinct_on_fields/tests.py8
2 files changed, 9 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 1484d9dca9..96ecdbc1f4 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -376,7 +376,7 @@ class QuerySet(object):
keyword arguments.
"""
clone = self.filter(*args, **kwargs)
- if self.query.can_filter():
+ if self.query.can_filter() and not self.query.distinct_fields:
clone = clone.order_by()
num = len(clone)
if num == 1:
diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py
index 68290d7b8f..9e53ce3ae9 100644
--- a/tests/distinct_on_fields/tests.py
+++ b/tests/distinct_on_fields/tests.py
@@ -129,3 +129,11 @@ class DistinctOnTests(TestCase):
qs, [self.p1_o2, self.p2_o1, self.p3_o1],
lambda x: x
)
+
+ def test_distinct_on_get_ordering_preserved(self):
+ """
+ Ordering shouldn't be cleared when distinct on fields are specified.
+ refs #25081
+ """
+ staff = Staff.objects.distinct('name').order_by('name', '-organisation').get(name='p1')
+ self.assertEqual(staff.organisation, 'o2')