Changeset 2997
- Timestamp:
- 05/26/06 18:41:43 (2 years ago)
- Files:
-
- django/trunk/django/db/models/query.py (modified) (5 diffs)
- django/trunk/tests/modeltests/or_lookups/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/query.py
r2992 r2997 292 292 def filter(self, *args, **kwargs): 293 293 "Returns a new QuerySet instance with the args ANDed to the existing set." 294 return self._filter_or_exclude( Q, *args, **kwargs)294 return self._filter_or_exclude(None, *args, **kwargs) 295 295 296 296 def exclude(self, *args, **kwargs): … … 298 298 return self._filter_or_exclude(QNot, *args, **kwargs) 299 299 300 def _filter_or_exclude(self, qtype, *args, **kwargs): 300 def _filter_or_exclude(self, mapper, *args, **kwargs): 301 # mapper is a callable used to transform Q objects, 302 # or None for identity transform 303 if mapper is None: 304 mapper = lambda x: x 301 305 if len(args) > 0 or len(kwargs) > 0: 302 306 assert self._limit is None and self._offset is None, \ … … 305 309 clone = self._clone() 306 310 if len(kwargs) > 0: 307 clone._filters = clone._filters & qtype(**kwargs)311 clone._filters = clone._filters & mapper(Q(**kwargs)) 308 312 if len(args) > 0: 309 clone._filters = clone._filters & reduce(operator.and_, args)313 clone._filters = clone._filters & reduce(operator.and_, map(mapper, args)) 310 314 return clone 311 315 … … 319 323 return self._filter_or_exclude(None, filter_obj) 320 324 else: 321 return self._filter_or_exclude( Q, **filter_obj)325 return self._filter_or_exclude(None, **filter_obj) 322 326 323 327 def select_related(self, true_or_false=True): … … 583 587 class QNot(Q): 584 588 "Encapsulates NOT (...) queries as objects" 589 def __init__(self, q): 590 "Creates a negation of the q object passed in." 591 self.q = q 585 592 586 593 def get_sql(self, opts): 587 tables, joins, where, params = s uper(QNot, self).get_sql(opts)594 tables, joins, where, params = self.q.get_sql(opts) 588 595 where2 = ['(NOT (%s))' % " AND ".join(where)] 589 596 return tables, joins, where2, params django/trunk/tests/modeltests/or_lookups/models.py
r2897 r2997 90 90 {1: Hello} 91 91 92 # Demonstrating exclude with a Q object 93 >>> Article.objects.exclude(Q(headline__startswith='Hello')) 94 [Goodbye] 95 92 96 # The 'complex_filter' method supports framework features such as 93 97 # 'limit_choices_to' which normally take a single dictionary of lookup arguments
