mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #31606 -- Allowed using condition with lookups in When() expression.
This commit is contained in:
committed by
Mariusz Felisiak
parent
2aac176e86
commit
587b179d41
@@ -6,7 +6,7 @@ from uuid import UUID
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.db.models import (
|
||||
BinaryField, Case, CharField, Count, DurationField, F,
|
||||
BinaryField, BooleanField, Case, CharField, Count, DurationField, F,
|
||||
GenericIPAddressField, IntegerField, Max, Min, Q, Sum, TextField,
|
||||
TimeField, UUIDField, Value, When,
|
||||
)
|
||||
@@ -312,6 +312,17 @@ class CaseExpressionTests(TestCase):
|
||||
transform=attrgetter('integer', 'integer2')
|
||||
)
|
||||
|
||||
def test_condition_with_lookups(self):
|
||||
qs = CaseTestModel.objects.annotate(
|
||||
test=Case(
|
||||
When(Q(integer2=1), string='2', then=Value(False)),
|
||||
When(Q(integer2=1), string='1', then=Value(True)),
|
||||
default=Value(False),
|
||||
output_field=BooleanField(),
|
||||
),
|
||||
)
|
||||
self.assertIs(qs.get(integer=1).test, True)
|
||||
|
||||
def test_case_reuse(self):
|
||||
SOME_CASE = Case(
|
||||
When(pk=0, then=Value('0')),
|
||||
@@ -1350,6 +1361,8 @@ class CaseWhenTests(SimpleTestCase):
|
||||
When(condition=object())
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
When(condition=Value(1, output_field=IntegerField()))
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
When(Value(1, output_field=IntegerField()), string='1')
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
When()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user