Ticket #1801: andbug.diff

File andbug.diff, 3.4 KB (added by mir@…, 18 years ago)

Patch for or tests to find this bug

  • 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
    a get_sql method).  
    1313
    1414from django.db import models
    1515
     16class Reporter(models.Model):
     17    first_name = models.CharField(maxlength=30)
     18    last_name = models.CharField(maxlength=30)
     19    email = models.EmailField()
     20
     21    def __repr__(self):
     22        return "%s %s" % (self.first_name, self.last_name)
     23
    1624class Article(models.Model):
    1725    headline = models.CharField(maxlength=50)
    1826    pub_date = models.DateTimeField()
     27    reporter = models.ForeignKey(Reporter)
    1928    class Meta:
    2029       ordering = ('pub_date',)
    2130
    API_TESTS = """  
    2635>>> from datetime import datetime
    2736>>> from django.db.models import Q
    2837
    29 >>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27))
     38>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
     39>>> r.save()
     40
     41>>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27), reporter=r)
    3042>>> a1.save()
    3143
    32 >>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
     44>>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28), reporter=r)
    3345>>> a2.save()
    3446
    35 >>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
     47>>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29), reporter=r)
    3648>>> a3.save()
    3749
    3850>>> Article.objects.filter(headline__startswith='Hello') |  Article.objects.filter(headline__startswith='Goodbye')
    API_TESTS = """  
    4961
    5062>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye')
    5163[Hello and goodbye]
     64
     65# Who has written a and a2?
     66>>> Reporter.objects.filter(article__id__exact=a1.id) & Reporter.objects.filter(article__id__exact=a2.id)
     67[John Smith]
    5268
    5369>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
    5470[Hello and goodbye]
    API_TESTS = """  
    6581>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
    6682[Hello, Goodbye, Hello and goodbye]
    6783
    68 # Q arg objects are ANDed 
     84# Q arg objects are ANDed
    6985>>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
    7086[Hello and goodbye]
    7187
    Hello and goodbye  
    81973
    8298
    8399>>> list(Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values())
    84 [{'headline': 'Hello and goodbye', 'pub_date': datetime.datetime(2005, 11, 29, 0, 0), 'id': 3}]
     100[{'headline': 'Hello and goodbye', 'reporter_id': 1, 'pub_date': datetime.datetime(2005, 11, 29, 0, 0), 'id': 3}]
    85101
    86102>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
    87103{1: Hello}
    88104
    89 # The 'complex_filter' method supports framework features such as 
     105# The 'complex_filter' method supports framework features such as
    90106# 'limit_choices_to' which normally take a single dictionary of lookup arguments
    91107# but need to support arbitrary queries via Q objects too.
    92108>>> Article.objects.complex_filter({'pk': 1})
Back to Top