Refs #29125 -- Made Q.deconstruct() omit 'query_utils' in the path and _connector='AND' since it's a default value.

This commit is contained in:
Tim Graham 2018-02-12 14:20:54 -05:00
parent b95c49c954
commit 9ba3df8240
2 changed files with 8 additions and 9 deletions

View File

@ -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

View File

@ -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'))