Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Using querysets as an rvalue in filter() calls was causing an unneces…


database query, due to a bool() call. This change stops that behaviour.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent d00c013 commit d068ad0c01be9bb085c86a4e0c4dd047a85ce18d @malcolmt malcolmt committed
8 django/db/models/
@@ -651,6 +651,10 @@ def as_sql(self):
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,6 +799,10 @@ def iterator(self):
# (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,
2  django/db/models/sql/
@@ -66,6 +66,8 @@ def add(self, data, connector):
# 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
annotation = bool(value)
7 tests/regressiontests/queries/
@@ -1015,6 +1015,13 @@ class PointerB(models.Model):
>>> 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
+>>> print Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy")).query
# In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__

0 comments on commit d068ad0

Please sign in to comment.
Something went wrong with that request. Please try again.