1 from __future__
import absolute_import
3 from datetime
import datetime
4 from operator
import attrgetter
6 from django
.test
import TestCase
8 from .models
import Article
, ArticlePKOrdering
11 class OrderingTests(TestCase
):
13 a1
= Article
.objects
.create(
14 headline
="Article 1", pub_date
=datetime(2005, 7, 26)
16 a2
= Article
.objects
.create(
17 headline
="Article 2", pub_date
=datetime(2005, 7, 27)
19 a3
= Article
.objects
.create(
20 headline
="Article 3", pub_date
=datetime(2005, 7, 27)
22 a4
= Article
.objects
.create(
23 headline
="Article 4", pub_date
=datetime(2005, 7, 28)
26 # By default, Article.objects.all() orders by pub_date descending, then
28 self
.assertQuerysetEqual(
29 Article
.objects
.all(), [
35 attrgetter("headline")
38 # Override ordering with order_by, which is in the same format as the
39 # ordering attribute in models.
40 self
.assertQuerysetEqual(
41 Article
.objects
.order_by("headline"), [
47 attrgetter("headline")
49 self
.assertQuerysetEqual(
50 Article
.objects
.order_by("pub_date", "-headline"), [
56 attrgetter("headline")
59 # Only the last order_by has any effect (since they each override any
61 self
.assertQuerysetEqual(
62 Article
.objects
.order_by("id"), [
68 attrgetter("headline")
70 self
.assertQuerysetEqual(
71 Article
.objects
.order_by("id").order_by("-headline"), [
77 attrgetter("headline")
80 # Use the 'stop' part of slicing notation to limit the results.
81 self
.assertQuerysetEqual(
82 Article
.objects
.order_by("headline")[:2], [
86 attrgetter("headline")
89 # Use the 'stop' and 'start' parts of slicing notation to offset the
91 self
.assertQuerysetEqual(
92 Article
.objects
.order_by("headline")[1:3], [
96 attrgetter("headline")
99 # Getting a single item should work too:
100 self
.assertEqual(Article
.objects
.all()[0], a4
)
102 # Use '?' to order randomly.
104 len(list(Article
.objects
.order_by("?"))), 4
107 # Ordering can be reversed using the reverse() method on a queryset.
108 # This allows you to extract things like "the last two items" (reverse
109 # and then take the first two).
110 self
.assertQuerysetEqual(
111 Article
.objects
.all().reverse()[:2], [
115 attrgetter("headline")
118 # Ordering can be based on fields included from an 'extra' clause
119 self
.assertQuerysetEqual(
120 Article
.objects
.extra(select
={"foo": "pub_date"}, order_by
=["foo", "headline"]), [
126 attrgetter("headline")
129 # If the extra clause uses an SQL keyword for a name, it will be
130 # protected by quoting.
131 self
.assertQuerysetEqual(
132 Article
.objects
.extra(select
={"order": "pub_date"}, order_by
=["order", "headline"]), [
138 attrgetter("headline")
141 def test_order_by_pk(self
):
143 Ensure that 'pk' works as an ordering option in Meta.
146 a1
= ArticlePKOrdering
.objects
.create(
147 pk
=1, headline
="Article 1", pub_date
=datetime(2005, 7, 26)
149 a2
= ArticlePKOrdering
.objects
.create(
150 pk
=2, headline
="Article 2", pub_date
=datetime(2005, 7, 27)
152 a3
= ArticlePKOrdering
.objects
.create(
153 pk
=3, headline
="Article 3", pub_date
=datetime(2005, 7, 27)
155 a4
= ArticlePKOrdering
.objects
.create(
156 pk
=4, headline
="Article 4", pub_date
=datetime(2005, 7, 28)
159 self
.assertQuerysetEqual(
160 ArticlePKOrdering
.objects
.all(), [
166 attrgetter("headline")