From aeb35548dc3a669aebec45f8f51a9cd3fbfc8801 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 12 Feb 2018 14:00:29 -0500 Subject: [PATCH] [2.0.x] Fixed #29125 -- Made Q.deconstruct() deterministic with multiple keyword arguments. Backport of b95c49c954e3b75678bb258e9fb2ec30d0d960bb from master --- django/db/models/query_utils.py | 2 +- docs/releases/2.0.3.txt | 4 ++++ tests/queries/test_q.py | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 8a889264e5..1ecc5e38c8 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -57,7 +57,7 @@ class Q(tree.Node): def __init__(self, *args, **kwargs): connector = kwargs.pop('_connector', None) negated = kwargs.pop('_negated', False) - super().__init__(children=list(args) + list(kwargs.items()), connector=connector, negated=negated) + super().__init__(children=list(args) + sorted(kwargs.items()), connector=connector, negated=negated) def _combine(self, other, conn): if not isinstance(other, Q): diff --git a/docs/releases/2.0.3.txt b/docs/releases/2.0.3.txt index 252e51fcf0..c0c9ebb782 100644 --- a/docs/releases/2.0.3.txt +++ b/docs/releases/2.0.3.txt @@ -17,3 +17,7 @@ Bugfixes (:ticket:`29109`). * Fixed crash with ``QuerySet.order_by(Exists(...))`` (:ticket:`29118`). + +* Made ``Q.deconstruct()`` deterministic with multiple keyword arguments + (:ticket:`29125`). You may need to modify ``Q``'s in existing migrations, or + accept an autogenerated migration. diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py index a90d6794db..2ed4278b77 100644 --- a/tests/queries/test_q.py +++ b/tests/queries/test_q.py @@ -60,6 +60,15 @@ class QTests(SimpleTestCase): )) self.assertEqual(kwargs, {'_connector': 'AND'}) + def test_deconstruct_multiple_kwargs(self): + q = Q(price__gt=F('discounted_price'), price=F('discounted_price')) + path, args, kwargs = q.deconstruct() + self.assertEqual(args, ( + ('price', F('discounted_price')), + ('price__gt', F('discounted_price')), + )) + self.assertEqual(kwargs, {'_connector': 'AND'}) + def test_deconstruct_nested(self): q = Q(Q(price__gt=F('discounted_price'))) path, args, kwargs = q.deconstruct()