1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #32548 -- Fixed crash when combining Q() objects with boolean expressions.

This commit is contained in:
Jonathan Richards
2021-03-14 14:00:40 -07:00
committed by Mariusz Felisiak
parent 54f60bc85d
commit 00b0786de5
3 changed files with 22 additions and 16 deletions

View File

@@ -1,6 +1,8 @@
from django.db.models import F, Q
from django.db.models import Exists, F, OuterRef, Q
from django.test import SimpleTestCase
from .models import Tag
class QTests(SimpleTestCase):
def test_combine_and_empty(self):
@@ -39,17 +41,14 @@ class QTests(SimpleTestCase):
q = Q(price__gt=F('discounted_price'))
path, args, kwargs = q.deconstruct()
self.assertEqual(path, 'django.db.models.Q')
self.assertEqual(args, ())
self.assertEqual(kwargs, {'price__gt': F('discounted_price')})
self.assertEqual(args, (('price__gt', F('discounted_price')),))
self.assertEqual(kwargs, {})
def test_deconstruct_negated(self):
q = ~Q(price__gt=F('discounted_price'))
path, args, kwargs = q.deconstruct()
self.assertEqual(args, ())
self.assertEqual(kwargs, {
'price__gt': F('discounted_price'),
'_negated': True,
})
self.assertEqual(args, (('price__gt', F('discounted_price')),))
self.assertEqual(kwargs, {'_negated': True})
def test_deconstruct_or(self):
q1 = Q(price__gt=F('discounted_price'))
@@ -88,6 +87,13 @@ class QTests(SimpleTestCase):
self.assertEqual(args, (Q(price__gt=F('discounted_price')),))
self.assertEqual(kwargs, {})
def test_deconstruct_boolean_expression(self):
tagged = Tag.objects.filter(category=OuterRef('pk'))
q = Q(Exists(tagged))
_, args, kwargs = q.deconstruct()
self.assertEqual(args, (Exists(tagged),))
self.assertEqual(kwargs, {})
def test_reconstruct(self):
q = Q(price__gt=F('discounted_price'))
path, args, kwargs = q.deconstruct()