Author: mtredinnick
Date: 2009-01-07 23:16:21 -0600 (Wed, 07 Jan 2009)
New Revision: 9715

Modified:
   django/trunk/django/db/models/query.py
   django/trunk/django/db/models/sql/where.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Using querysets as an rvalue in filter() calls was causing an unnecessary
database query, due to a bool() call. This change stops that behaviour.

Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2009-01-08 05:15:41 UTC (rev 
9714)
+++ django/trunk/django/db/models/query.py      2009-01-08 05:16:21 UTC (rev 
9715)
@@ -651,6 +651,10 @@
         obj = self.values("pk")
         return obj.query.as_nested_sql()
 
+    # When used as part of a nested query, a queryset will never be an "always
+    # empty" result.
+    value_annotation = True
+
 class ValuesQuerySet(QuerySet):
     def __init__(self, *args, **kwargs):
         super(ValuesQuerySet, self).__init__(*args, **kwargs)
@@ -795,7 +799,11 @@
         # (it raises StopIteration immediately).
         yield iter([]).next()
 
+    # EmptyQuerySet is always an empty result in where-clauses (and similar
+    # situations).
+    value_annotation = False
 
+
 def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
                    requested=None):
     """

Modified: django/trunk/django/db/models/sql/where.py
===================================================================
--- django/trunk/django/db/models/sql/where.py  2009-01-08 05:15:41 UTC (rev 
9714)
+++ django/trunk/django/db/models/sql/where.py  2009-01-08 05:16:21 UTC (rev 
9715)
@@ -66,6 +66,8 @@
         # here in the future (using Python types is suggested for consistency).
         if isinstance(value, datetime.datetime):
             annotation = datetime.datetime
+        elif hasattr(value, 'value_annotation'):
+            annotation = value.value_annotation
         else:
             annotation = bool(value)
 

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2009-01-08 
05:15:41 UTC (rev 9714)
+++ django/trunk/tests/regressiontests/queries/models.py        2009-01-08 
05:16:21 UTC (rev 9715)
@@ -1015,6 +1015,13 @@
 >>> Annotation.objects.filter(notes__in=Note.objects.filter(note="n1"))
 [<Annotation: a1>]
 
+Nested queries should not evaluate the inner query as part of constructing the
+SQL. This test verifies this: if the inner query is evaluated, the outer "in"
+lookup will raise an EmptyResultSet exception (as the inner query returns
+nothing).
+>>> print 
Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy")).query
+SELECT ...
+
 """}
 
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__


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

Reply via email to