diff options
| author | starryrbs <1322096624@qq.com> | 2021-02-18 00:04:59 +0800 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-02-18 22:06:36 +0100 |
| commit | 0e2979e95d0f68f28c92c84c14655e7510d4e789 (patch) | |
| tree | 2c8a2da3e45107b28f03a1c7bd5c05822b719c75 | |
| parent | 6897da60961878348df298193e416d2ea4f910b3 (diff) | |
[3.2.x] Fixed #32450 -- Fixed crash when ANDing/ORing an empty Q() with not pickleable Q().
Regression in bb0b6e526340e638522e093765e534df4e4393d2.
Backport of 466920f6d726eee90d5566e0a9948e92b33a122e from master
| -rw-r--r-- | django/db/models/query_utils.py | 7 | ||||
| -rw-r--r-- | tests/queries/test_q.py | 8 |
2 files changed, 12 insertions, 3 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index f7c6d74e72..c2b693ab8d 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -5,7 +5,6 @@ Factored out from django.db.models.query to avoid making the main module very large and/or so that they can be used by other modules without getting into circular import difficulties. """ -import copy import functools import inspect import warnings @@ -74,10 +73,12 @@ class Q(tree.Node): # If the other Q() is empty, ignore it and just use `self`. if not other: - return copy.deepcopy(self) + _, args, kwargs = self.deconstruct() + return type(self)(*args, **kwargs) # Or if this Q is empty, ignore it and just use `other`. elif not self: - return copy.deepcopy(other) + _, args, kwargs = other.deconstruct() + return type(other)(*args, **kwargs) obj = type(self)() obj.connector = conn diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py index 9adff07ef2..6dcf36ce02 100644 --- a/tests/queries/test_q.py +++ b/tests/queries/test_q.py @@ -8,6 +8,10 @@ class QTests(SimpleTestCase): self.assertEqual(q & Q(), q) self.assertEqual(Q() & q, q) + q = Q(x__in={}.keys()) + self.assertEqual(q & Q(), q) + self.assertEqual(Q() & q, q) + def test_combine_and_both_empty(self): self.assertEqual(Q() & Q(), Q()) @@ -16,6 +20,10 @@ class QTests(SimpleTestCase): self.assertEqual(q | Q(), q) self.assertEqual(Q() | q, q) + q = Q(x__in={}.keys()) + self.assertEqual(q | Q(), q) + self.assertEqual(Q() | q, q) + def test_combine_or_both_empty(self): self.assertEqual(Q() | Q(), Q()) |
