mirror of
https://github.com/django/django.git
synced 2025-08-29 06:54:40 +00:00
Fixed #24420 -- Allowed ordering by case expressions
This commit is contained in:
parent
82f7bee1d5
commit
ceaf31adff
3 changed files with 47 additions and 0 deletions
|
@ -980,6 +980,30 @@ class CaseExpressionTests(TestCase):
|
|||
transform=attrgetter('integer', 'integer2', 'test')
|
||||
)
|
||||
|
||||
def test_order_by_conditional_implicit(self):
|
||||
self.assertQuerysetEqual(
|
||||
CaseTestModel.objects.filter(integer__lte=2).annotate(test=Case(
|
||||
When(integer=1, then=2),
|
||||
When(integer=2, then=1),
|
||||
default=3,
|
||||
output_field=models.IntegerField(),
|
||||
)).order_by('test', 'pk'),
|
||||
[(2, 1), (2, 1), (1, 2)],
|
||||
transform=attrgetter('integer', 'test')
|
||||
)
|
||||
|
||||
def test_order_by_conditional_explicit(self):
|
||||
self.assertQuerysetEqual(
|
||||
CaseTestModel.objects.filter(integer__lte=2).annotate(test=Case(
|
||||
When(integer=1, then=2),
|
||||
When(integer=2, then=1),
|
||||
default=3,
|
||||
output_field=models.IntegerField(),
|
||||
)).order_by(F('test').asc(), 'pk'),
|
||||
[(2, 1), (2, 1), (1, 2)],
|
||||
transform=attrgetter('integer', 'test')
|
||||
)
|
||||
|
||||
|
||||
class CaseDocumentationExamples(TestCase):
|
||||
@classmethod
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue