From d56c1ab7f07eeaae3a0fc996169be7cae6a94bf4 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 16 Sep 2009 12:09:47 +0000 Subject: [PATCH] Fixed #11886 -- Corrected handling of F() expressions that use parentheses. Thanks to Brent Hagany for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11581 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/expressions.py | 2 +- tests/modeltests/expressions/models.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/expressions.py b/django/db/models/sql/expressions.py index 920cbffe73d..0914c2b3c14 100644 --- a/django/db/models/sql/expressions.py +++ b/django/db/models/sql/expressions.py @@ -66,7 +66,7 @@ class SQLEvaluator(object): else: sql, params = '%s', (child,) - if hasattr(child, 'children') > 1: + if len(getattr(child, 'children', [])) > 1: format = '(%s)' else: format = '%s' diff --git a/tests/modeltests/expressions/models.py b/tests/modeltests/expressions/models.py index 27daabad713..b515682a9c3 100644 --- a/tests/modeltests/expressions/models.py +++ b/tests/modeltests/expressions/models.py @@ -56,6 +56,16 @@ __test__ = {'API_TESTS': """ >>> company_query [{'num_chairs': 2302, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 5, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 34, 'name': u'Test GmbH', 'num_employees': 32}] +# Law of order of operations is followed +>>> _ =company_query.update(num_chairs=F('num_employees') + 2 * F('num_employees')) +>>> company_query +[{'num_chairs': 6900, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 9, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 96, 'name': u'Test GmbH', 'num_employees': 32}] + +# Law of order of operations can be overridden by parentheses +>>> _ =company_query.update(num_chairs=((F('num_employees') + 2) * F('num_employees'))) +>>> company_query +[{'num_chairs': 5294600, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 15, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 1088, 'name': u'Test GmbH', 'num_employees': 32}] + # The relation of a foreign key can become copied over to an other foreign key. >>> Company.objects.update(point_of_contact=F('ceo')) 3