Ticket #1803: union_set.2.diff

File union_set.2.diff, 3.3 KB (added by mir@…, 19 years ago)

patch, this time probably trac-readable. Replaces other patch.

  • django/db/models/query.py

    a b class QuerySet(object):  
    140140        combined._filters = self._filters & other._filters
    141141        return combined
    142142
     143    # add set-like intersection, it is only an alias for __and__
     144    intersection = __and__
     145
    143146    def __or__(self, other):
    144147        combined = self._combine(other)
    145148        combined._filters = self._filters | other._filters
    146149        return combined
     150
     151    # add set-like union, it is only an alias for __or__
     152    union = __or__
    147153
    148154    ####################################
    149155    # METHODS THAT DO DATABASE QUERIES #
    class QuerySet(object):  
    302308
    303309    def complex_filter(self, filter_obj):
    304310        """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
    306312        keyword lookup arguments."""
    307313        # This exists to support framework features such as 'limit_choices_to',
    308314        # and usually it will be more natural to use other methods.
  • tests/modeltests/or_lookups/models.py

    a b  
    11"""
    2219. OR lookups
    33
    4 To perform an OR lookup, or a lookup that combines ANDs and ORs, 
     4To perform an OR lookup, or a lookup that combines ANDs and ORs,
    55combine QuerySet objects using & and | operators.
    66
    7 Alternatively, use positional arguments, and pass one or more expressions 
     7Alternatively, use positional arguments, and pass one or more expressions
    88of clauses using the variable ``django.db.models.Q`` (or any object with
    99a get_sql method).
    1010
    API_TESTS = """  
    3838>>> Article.objects.filter(headline__startswith='Hello') |  Article.objects.filter(headline__startswith='Goodbye')
    3939[Hello, Goodbye, Hello and goodbye]
    4040
     41>>> Article.objects.filter(headline__startswith='Hello').union(Article.objects.filter(headline__startswith='Goodbye'))
     42[Hello, Goodbye, Hello and goodbye]
     43
    4144>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye'))
    4245[Hello, Goodbye, Hello and goodbye]
    4346
    API_TESTS = """  
    4548[]
    4649
    4750>>> 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'))
    4854[]
    4955
    5056>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye')
    API_TESTS = """  
    6571>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
    6672[Hello, Goodbye, Hello and goodbye]
    6773
    68 # Q arg objects are ANDed 
     74# Q arg objects are ANDed
    6975>>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
    7076[Hello and goodbye]
    7177
    Hello and goodbye  
    8692>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
    8793{1: Hello}
    8894
    89 # The 'complex_filter' method supports framework features such as 
     95# The 'complex_filter' method supports framework features such as
    9096# 'limit_choices_to' which normally take a single dictionary of lookup arguments
    9197# but need to support arbitrary queries via Q objects too.
    9298>>> Article.objects.complex_filter({'pk': 1})
Back to Top