Ticket #1801: andbug.diff
File andbug.diff, 3.4 KB (added by , 19 years ago) |
---|
-
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 … … a get_sql method). 13 13 14 14 from django.db import models 15 15 16 class 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 16 24 class Article(models.Model): 17 25 headline = models.CharField(maxlength=50) 18 26 pub_date = models.DateTimeField() 27 reporter = models.ForeignKey(Reporter) 19 28 class Meta: 20 29 ordering = ('pub_date',) 21 30 … … API_TESTS = """ 26 35 >>> from datetime import datetime 27 36 >>> from django.db.models import Q 28 37 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) 30 42 >>> a1.save() 31 43 32 >>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28) )44 >>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28), reporter=r) 33 45 >>> a2.save() 34 46 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) 36 48 >>> a3.save() 37 49 38 50 >>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye') … … API_TESTS = """ 49 61 50 62 >>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye') 51 63 [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] 52 68 53 69 >>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello') 54 70 [Hello and goodbye] … … API_TESTS = """ 65 81 >>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) 66 82 [Hello, Goodbye, Hello and goodbye] 67 83 68 # Q arg objects are ANDed 84 # Q arg objects are ANDed 69 85 >>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')) 70 86 [Hello and goodbye] 71 87 … … Hello and goodbye 81 97 3 82 98 83 99 >>> 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}] 85 101 86 102 >>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2]) 87 103 {1: Hello} 88 104 89 # The 'complex_filter' method supports framework features such as 105 # The 'complex_filter' method supports framework features such as 90 106 # 'limit_choices_to' which normally take a single dictionary of lookup arguments 91 107 # but need to support arbitrary queries via Q objects too. 92 108 >>> Article.objects.complex_filter({'pk': 1})