Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug with QuerySet.exclude() failing to do negations on Q object…

…s, and

at the same time generalised exclude/QNot so that they work for 'external'
Q objects i.e. ones that simply have 'get_sql' defined.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@2997 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 60c3e55b1f7c87ef9fa7c5ffdaf6e8dba5ab7043 1 parent c3baf46
Luke Plant spookylukey authored
Showing with 17 additions and 6 deletions.
  1. +13 −6 django/db/models/query.py
  2. +4 −0 tests/modeltests/or_lookups/models.py
19 django/db/models/query.py
View
@@ -291,22 +291,26 @@ def dates(self, field_name, kind, order='ASC'):
def filter(self, *args, **kwargs):
"Returns a new QuerySet instance with the args ANDed to the existing set."
- return self._filter_or_exclude(Q, *args, **kwargs)
+ return self._filter_or_exclude(None, *args, **kwargs)
def exclude(self, *args, **kwargs):
"Returns a new QuerySet instance with NOT (args) ANDed to the existing set."
return self._filter_or_exclude(QNot, *args, **kwargs)
- def _filter_or_exclude(self, qtype, *args, **kwargs):
+ def _filter_or_exclude(self, mapper, *args, **kwargs):
+ # mapper is a callable used to transform Q objects,
+ # or None for identity transform
+ if mapper is None:
+ mapper = lambda x: x
if len(args) > 0 or len(kwargs) > 0:
assert self._limit is None and self._offset is None, \
"Cannot filter a query once a slice has been taken."
clone = self._clone()
if len(kwargs) > 0:
- clone._filters = clone._filters & qtype(**kwargs)
+ clone._filters = clone._filters & mapper(Q(**kwargs))
if len(args) > 0:
- clone._filters = clone._filters & reduce(operator.and_, args)
+ clone._filters = clone._filters & reduce(operator.and_, map(mapper, args))
return clone
def complex_filter(self, filter_obj):
@@ -318,7 +322,7 @@ def complex_filter(self, filter_obj):
if hasattr(filter_obj, 'get_sql'):
return self._filter_or_exclude(None, filter_obj)
else:
- return self._filter_or_exclude(Q, **filter_obj)
+ return self._filter_or_exclude(None, **filter_obj)
def select_related(self, true_or_false=True):
"Returns a new QuerySet instance with '_select_related' modified."
@@ -582,9 +586,12 @@ def get_sql(self, opts):
class QNot(Q):
"Encapsulates NOT (...) queries as objects"
+ def __init__(self, q):
+ "Creates a negation of the q object passed in."
+ self.q = q
def get_sql(self, opts):
- tables, joins, where, params = super(QNot, self).get_sql(opts)
+ tables, joins, where, params = self.q.get_sql(opts)
where2 = ['(NOT (%s))' % " AND ".join(where)]
return tables, joins, where2, params
4 tests/modeltests/or_lookups/models.py
View
@@ -89,6 +89,10 @@ def __repr__(self):
>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
{1: Hello}
+# Demonstrating exclude with a Q object
+>>> Article.objects.exclude(Q(headline__startswith='Hello'))
+[Goodbye]
+
# The 'complex_filter' method supports framework features such as
# 'limit_choices_to' which normally take a single dictionary of lookup arguments
# but need to support arbitrary queries via Q objects too.
Please sign in to comment.
Something went wrong with that request. Please try again.