Author: russellm
Date: 2009-09-16 07:09:47 -0500 (Wed, 16 Sep 2009)
New Revision: 11581

Modified:
   django/trunk/django/db/models/sql/expressions.py
   django/trunk/tests/modeltests/expressions/models.py
Log:
Fixed #11886 -- Corrected handling of F() expressions that use parentheses. 
Thanks to Brent Hagany for the report.

Modified: django/trunk/django/db/models/sql/expressions.py
===================================================================
--- django/trunk/django/db/models/sql/expressions.py    2009-09-14 22:49:03 UTC 
(rev 11580)
+++ django/trunk/django/db/models/sql/expressions.py    2009-09-16 12:09:47 UTC 
(rev 11581)
@@ -66,7 +66,7 @@
             else:
                 sql, params = '%s', (child,)
 
-            if hasattr(child, 'children') > 1:
+            if len(getattr(child, 'children', [])) > 1:
                 format = '(%s)'
             else:
                 format = '%s'

Modified: django/trunk/tests/modeltests/expressions/models.py
===================================================================
--- django/trunk/tests/modeltests/expressions/models.py 2009-09-14 22:49:03 UTC 
(rev 11580)
+++ django/trunk/tests/modeltests/expressions/models.py 2009-09-16 12:09:47 UTC 
(rev 11581)
@@ -56,6 +56,16 @@
 >>> 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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to