Ticket #1803: union_set.2.diff
File union_set.2.diff, 3.3 KB (added by , 19 years ago) |
---|
-
django/db/models/query.py
a b class QuerySet(object): 140 140 combined._filters = self._filters & other._filters 141 141 return combined 142 142 143 # add set-like intersection, it is only an alias for __and__ 144 intersection = __and__ 145 143 146 def __or__(self, other): 144 147 combined = self._combine(other) 145 148 combined._filters = self._filters | other._filters 146 149 return combined 150 151 # add set-like union, it is only an alias for __or__ 152 union = __or__ 147 153 148 154 #################################### 149 155 # METHODS THAT DO DATABASE QUERIES # … … class QuerySet(object): 302 308 303 309 def complex_filter(self, filter_obj): 304 310 """Returns a new QuerySet instance with filter_obj added to the filters. 305 filter_obj can be a Q object (has 'get_sql' method) or a dictionary of 311 filter_obj can be a Q object (has 'get_sql' method) or a dictionary of 306 312 keyword lookup arguments.""" 307 313 # This exists to support framework features such as 'limit_choices_to', 308 314 # and usually it will be more natural to use other methods. -
tests/modeltests/or_lookups/models.py
a b 1 1 """ 2 2 19. OR lookups 3 3 4 To perform an OR lookup, or a lookup that combines ANDs and ORs, 4 To perform an OR lookup, or a lookup that combines ANDs and ORs, 5 5 combine QuerySet objects using & and | operators. 6 6 7 Alternatively, use positional arguments, and pass one or more expressions 7 Alternatively, use positional arguments, and pass one or more expressions 8 8 of clauses using the variable ``django.db.models.Q`` (or any object with 9 9 a get_sql method). 10 10 … … API_TESTS = """ 38 38 >>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye') 39 39 [Hello, Goodbye, Hello and goodbye] 40 40 41 >>> Article.objects.filter(headline__startswith='Hello').union(Article.objects.filter(headline__startswith='Goodbye')) 42 [Hello, Goodbye, Hello and goodbye] 43 41 44 >>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')) 42 45 [Hello, Goodbye, Hello and goodbye] 43 46 … … API_TESTS = """ 45 48 [] 46 49 47 50 >>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__startswith='Goodbye') 51 [] 52 53 >>> Article.objects.filter(headline__startswith='Hello').intersection(Article.objects.filter(headline__startswith='Goodbye')) 48 54 [] 49 55 50 56 >>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye') … … API_TESTS = """ 65 71 >>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) 66 72 [Hello, Goodbye, Hello and goodbye] 67 73 68 # Q arg objects are ANDed 74 # Q arg objects are ANDed 69 75 >>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')) 70 76 [Hello and goodbye] 71 77 … … Hello and goodbye 86 92 >>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2]) 87 93 {1: Hello} 88 94 89 # The 'complex_filter' method supports framework features such as 95 # The 'complex_filter' method supports framework features such as 90 96 # 'limit_choices_to' which normally take a single dictionary of lookup arguments 91 97 # but need to support arbitrary queries via Q objects too. 92 98 >>> Article.objects.complex_filter({'pk': 1})