From 9ba3df82402e7e23b353da20aea6894935241ef9 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 12 Feb 2018 14:20:54 -0500 Subject: [PATCH] Refs #29125 -- Made Q.deconstruct() omit 'query_utils' in the path and _connector='AND' since it's a default value. --- django/db/models/query_utils.py | 5 ++++- tests/queries/test_q.py | 12 ++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index e34963b65c..8c41116d69 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -98,13 +98,16 @@ class Q(tree.Node): def deconstruct(self): 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) - kwargs = {'_connector': self.connector} + if self.connector != self.default: + kwargs = {'_connector': self.connector} if self.negated: kwargs['_negated'] = True return path, args, kwargs diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py index 2ed4278b77..9e71553ef7 100644 --- a/tests/queries/test_q.py +++ b/tests/queries/test_q.py @@ -22,14 +22,13 @@ class QTests(SimpleTestCase): def test_deconstruct(self): q = Q(price__gt=F('discounted_price')) path, args, kwargs = q.deconstruct() - self.assertEqual(path, 'django.db.models.query_utils.Q') + self.assertEqual(path, 'django.db.models.Q') self.assertEqual(args, ()) self.assertEqual(kwargs, {'price__gt': F('discounted_price')}) def test_deconstruct_negated(self): q = ~Q(price__gt=F('discounted_price')) path, args, kwargs = q.deconstruct() - self.assertEqual(path, 'django.db.models.query_utils.Q') self.assertEqual(args, ()) self.assertEqual(kwargs, { 'price__gt': F('discounted_price'), @@ -41,7 +40,6 @@ class QTests(SimpleTestCase): q2 = Q(price=F('discounted_price')) q = q1 | q2 path, args, kwargs = q.deconstruct() - self.assertEqual(path, 'django.db.models.query_utils.Q') self.assertEqual(args, ( ('price__gt', F('discounted_price')), ('price', F('discounted_price')), @@ -53,12 +51,11 @@ class QTests(SimpleTestCase): q2 = Q(price=F('discounted_price')) q = q1 & q2 path, args, kwargs = q.deconstruct() - self.assertEqual(path, 'django.db.models.query_utils.Q') self.assertEqual(args, ( ('price__gt', F('discounted_price')), ('price', F('discounted_price')), )) - self.assertEqual(kwargs, {'_connector': 'AND'}) + self.assertEqual(kwargs, {}) def test_deconstruct_multiple_kwargs(self): q = Q(price__gt=F('discounted_price'), price=F('discounted_price')) @@ -67,14 +64,13 @@ class QTests(SimpleTestCase): ('price', F('discounted_price')), ('price__gt', F('discounted_price')), )) - self.assertEqual(kwargs, {'_connector': 'AND'}) + self.assertEqual(kwargs, {}) def test_deconstruct_nested(self): q = Q(Q(price__gt=F('discounted_price'))) path, args, kwargs = q.deconstruct() - self.assertEqual(path, 'django.db.models.query_utils.Q') self.assertEqual(args, (Q(price__gt=F('discounted_price')),)) - self.assertEqual(kwargs, {'_connector': 'AND'}) + self.assertEqual(kwargs, {}) def test_reconstruct(self): q = Q(price__gt=F('discounted_price'))