[1.11.x] Fixed #28722 -- Made QuerySet.reverse() affect nulls_first/nulls_last.
Backport of 21a3a29dc9
from master
This commit is contained in:
parent
b0566e5bb3
commit
e98ae4fe6b
1
AUTHORS
1
AUTHORS
|
@ -765,6 +765,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Tome Cvitan <tome@cvitan.com>
|
Tome Cvitan <tome@cvitan.com>
|
||||||
Tomek Paczkowski <tomek@hauru.eu>
|
Tomek Paczkowski <tomek@hauru.eu>
|
||||||
Tom Insam
|
Tom Insam
|
||||||
|
Tomer Chachamu
|
||||||
Tommy Beadle <tbeadle@gmail.com>
|
Tommy Beadle <tbeadle@gmail.com>
|
||||||
Tom Tobin
|
Tom Tobin
|
||||||
Tore Lundqvist <tore.lundqvist@gmail.com>
|
Tore Lundqvist <tore.lundqvist@gmail.com>
|
||||||
|
|
|
@ -1089,6 +1089,9 @@ class OrderBy(BaseExpression):
|
||||||
|
|
||||||
def reverse_ordering(self):
|
def reverse_ordering(self):
|
||||||
self.descending = not self.descending
|
self.descending = not self.descending
|
||||||
|
if self.nulls_first or self.nulls_last:
|
||||||
|
self.nulls_first = not self.nulls_first
|
||||||
|
self.nulls_last = not self.nulls_last
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def asc(self):
|
def asc(self):
|
||||||
|
|
|
@ -13,3 +13,6 @@ Bugfixes
|
||||||
argument is a callable that returns ``None`` (:ticket:`28601`).
|
argument is a callable that returns ``None`` (:ticket:`28601`).
|
||||||
|
|
||||||
* Fixed the Basque ``DATE_FORMAT`` string (:ticket:`28710`).
|
* Fixed the Basque ``DATE_FORMAT`` string (:ticket:`28710`).
|
||||||
|
|
||||||
|
* Made ``QuerySet.reverse()`` affect ``nulls_first`` and ``nulls_last``
|
||||||
|
(:ticket:`28722`).
|
||||||
|
|
|
@ -94,24 +94,28 @@ class OrderingTests(TestCase):
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
Article.objects.order_by(F("author").desc(nulls_last=True, nulls_first=True))
|
Article.objects.order_by(F("author").desc(nulls_last=True, nulls_first=True))
|
||||||
|
|
||||||
|
def assertQuerysetEqualReversible(self, queryset, sequence):
|
||||||
|
self.assertSequenceEqual(queryset, sequence)
|
||||||
|
self.assertSequenceEqual(queryset.reverse(), list(reversed(sequence)))
|
||||||
|
|
||||||
def test_order_by_nulls_last(self):
|
def test_order_by_nulls_last(self):
|
||||||
Article.objects.filter(headline="Article 3").update(author=self.author_1)
|
Article.objects.filter(headline="Article 3").update(author=self.author_1)
|
||||||
Article.objects.filter(headline="Article 4").update(author=self.author_2)
|
Article.objects.filter(headline="Article 4").update(author=self.author_2)
|
||||||
# asc and desc are chainable with nulls_last.
|
# asc and desc are chainable with nulls_last.
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(F("author").desc(nulls_last=True)),
|
Article.objects.order_by(F("author").desc(nulls_last=True), 'headline'),
|
||||||
[self.a4, self.a3, self.a1, self.a2],
|
[self.a4, self.a3, self.a1, self.a2],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(F("author").asc(nulls_last=True)),
|
Article.objects.order_by(F("author").asc(nulls_last=True), 'headline'),
|
||||||
[self.a3, self.a4, self.a1, self.a2],
|
[self.a3, self.a4, self.a1, self.a2],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(Upper("author__name").desc(nulls_last=True)),
|
Article.objects.order_by(Upper("author__name").desc(nulls_last=True), 'headline'),
|
||||||
[self.a4, self.a3, self.a1, self.a2],
|
[self.a4, self.a3, self.a1, self.a2],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(Upper("author__name").asc(nulls_last=True)),
|
Article.objects.order_by(Upper("author__name").asc(nulls_last=True), 'headline'),
|
||||||
[self.a3, self.a4, self.a1, self.a2],
|
[self.a3, self.a4, self.a1, self.a2],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -119,20 +123,20 @@ class OrderingTests(TestCase):
|
||||||
Article.objects.filter(headline="Article 3").update(author=self.author_1)
|
Article.objects.filter(headline="Article 3").update(author=self.author_1)
|
||||||
Article.objects.filter(headline="Article 4").update(author=self.author_2)
|
Article.objects.filter(headline="Article 4").update(author=self.author_2)
|
||||||
# asc and desc are chainable with nulls_first.
|
# asc and desc are chainable with nulls_first.
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(F("author").asc(nulls_first=True)),
|
Article.objects.order_by(F("author").asc(nulls_first=True), 'headline'),
|
||||||
[self.a1, self.a2, self.a3, self.a4],
|
[self.a1, self.a2, self.a3, self.a4],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(F("author").desc(nulls_first=True)),
|
Article.objects.order_by(F("author").desc(nulls_first=True), 'headline'),
|
||||||
[self.a1, self.a2, self.a4, self.a3],
|
[self.a1, self.a2, self.a4, self.a3],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(Upper("author__name").asc(nulls_first=True)),
|
Article.objects.order_by(Upper("author__name").asc(nulls_first=True), 'headline'),
|
||||||
[self.a1, self.a2, self.a3, self.a4],
|
[self.a1, self.a2, self.a3, self.a4],
|
||||||
)
|
)
|
||||||
self.assertSequenceEqual(
|
self.assertQuerysetEqualReversible(
|
||||||
Article.objects.order_by(Upper("author__name").desc(nulls_first=True)),
|
Article.objects.order_by(Upper("author__name").desc(nulls_first=True), 'headline'),
|
||||||
[self.a1, self.a2, self.a4, self.a3],
|
[self.a1, self.a2, self.a4, self.a3],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue