diff options
author | Jonathan Richards <jonathan@golorry.com> | 2021-03-14 14:00:40 -0700 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-04-14 19:46:45 +0200 |
commit | d0267690f8a8e83065459d13a5a6f29e75640f78 (patch) | |
tree | 0002b1dc6c6638c1a86f65de2e367cb0b4ded30d | |
parent | 65dfb06a1ab56c238cc80f5e1c31f61210c4577d (diff) | |
download | django-d0267690f8a8e83065459d13a5a6f29e75640f78.tar.gz |
[3.2.x] Fixed #32548 -- Fixed crash when combining Q() objects with boolean expressions.
Backport of 00b0786de533dbb3f6208d8d5eaddbf765b4e5b8 from main.
Regression in 466920f6d726eee90d5566e0a9948e92b33a122e.
-rw-r--r-- | django/db/models/query_utils.py | 12 | ||||
-rw-r--r-- | docs/releases/3.2.1.txt | 3 | ||||
-rw-r--r-- | tests/expressions/tests.py | 4 | ||||
-rw-r--r-- | tests/queries/test_q.py | 22 |
4 files changed, 25 insertions, 16 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 3beca7ce30..04564e5a0d 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -112,14 +112,10 @@ class Q(tree.Node): path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__) if path.startswith('django.db.models.query_utils'): path = path.replace('django.db.models.query_utils', 'django.db.models') - args, kwargs = (), {} - if len(self.children) == 1 and not isinstance(self.children[0], Q): - child = self.children[0] - kwargs = {child[0]: child[1]} - else: - args = tuple(self.children) - if self.connector != self.default: - kwargs = {'_connector': self.connector} + args = tuple(self.children) + kwargs = {} + if self.connector != self.default: + kwargs['_connector'] = self.connector if self.negated: kwargs['_negated'] = True return path, args, kwargs diff --git a/docs/releases/3.2.1.txt b/docs/releases/3.2.1.txt index 4ad9eff5ca..df8d192327 100644 --- a/docs/releases/3.2.1.txt +++ b/docs/releases/3.2.1.txt @@ -33,3 +33,6 @@ Bugfixes * Fixed a bug in Django 3.2 where variable lookup errors were logged rendering the sitemap template if alternates were not defined (:ticket:`32648`). + +* Fixed a regression in Django 3.2 that caused a crash when combining ``Q()`` + objects which contains boolean expressions (:ticket:`32548`). diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 1c32ab045b..9a34242de7 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -821,6 +821,10 @@ class BasicExpressionsTests(TestCase): Q() & Exists(is_poc), Exists(is_poc) | Q(), Q() | Exists(is_poc), + Q(Exists(is_poc)) & Q(), + Q() & Q(Exists(is_poc)), + Q(Exists(is_poc)) | Q(), + Q() | Q(Exists(is_poc)), ] for conditions in tests: with self.subTest(conditions): diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py index 6dcf36ce02..24a705f07f 100644 --- a/tests/queries/test_q.py +++ b/tests/queries/test_q.py @@ -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() |