Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a `QuerySet.ordered` property to check if a queryset is already…

… ordered. Refs #10163.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10623 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c00e8d20641910e0fdbce9f155e39b28bb824672 1 parent d463580
@jacobian jacobian authored
View
17 django/db/models/query.py
@@ -615,7 +615,24 @@ def only(self, *fields):
clone = self._clone()
clone.query.add_immediate_loading(fields)
return clone
+
+ ###################################
+ # PUBLIC INTROSPECTION ATTRIBUTES #
+ ###################################
+ def ordered(self):
+ """
+ Returns True if the QuerySet is ordered -- i.e. has an order_by()
+ clause or a default ordering on the model.
+ """
+ if self.query.extra_order_by or self.query.order_by:
+ return True
+ elif self.query.default_ordering and self.query.model._meta.ordering:
+ return True
+ else:
+ return False
+ ordered = property(ordered)
+
###################
# PRIVATE METHODS #
###################
View
6 docs/ref/models/querysets.txt
@@ -268,6 +268,12 @@ There's no way to specify whether ordering should be case sensitive. With
respect to case-sensitivity, Django will order results however your database
backend normally orders them.
+.. versionadded:: 1.1
+
+You can tell if a query is ordered or not by checking the
+:attr:`QuerySet.ordered` attribute, which will be ``True`` if the
+``QuerySet`` has been ordered in any way.
+
``reverse()``
~~~~~~~~~~~~~
View
27 tests/regressiontests/queries/tests.py
@@ -0,0 +1,27 @@
+import unittest
+from models import Tag, Annotation
+from django.db.models import Count
+
+class QuerysetOrderedTests(unittest.TestCase):
+ """
+ Tests for the Queryset.ordered attribute.
+ """
+
+ def test_no_default_or_explicit_ordering(self):
+ self.assertEqual(Annotation.objects.all().ordered, False)
+
+ def test_cleared_default_ordering(self):
+ self.assertEqual(Tag.objects.all().ordered, True)
+ self.assertEqual(Tag.objects.all().order_by().ordered, False)
+
+ def test_explicit_ordering(self):
+ self.assertEqual(Annotation.objects.all().order_by('id').ordered, True)
+
+ def test_order_by_extra(self):
+ self.assertEqual(Annotation.objects.all().extra(order_by=['id']).ordered, True)
+
+ def test_annotated_ordering(self):
+ qs = Annotation.objects.annotate(num_notes=Count('notes'))
+ self.assertEqual(qs.ordered, False)
+ self.assertEqual(qs.order_by('num_notes').ordered, True)
+

0 comments on commit c00e8d2

Please sign in to comment.
Something went wrong with that request. Please try again.