Code

Ticket #2080: m2o_or_lookup_test.diff

File m2o_or_lookup_test.diff, 1.3 KB (added by akaihola, 8 years ago)

a test case for an OR lookup across a foreign key relation

Line 
1Index: modeltests/many_to_one/models.py
2===================================================================
3--- modeltests/many_to_one/models.py    (revision 3077)
4+++ modeltests/many_to_one/models.py    (working copy)
5@@ -217,6 +217,21 @@
6 >>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
7 [<Reporter: John Smith>]
8 
9+# OR lookups across foreign keys
10+#
11+# At revision 3077, the last test case fails to find Luke.  An inner
12+# join is created, and since Luke has no articles, he doesn't match
13+# the criteria.  For someone not familiar with how the ORM works, it's
14+# quite unexpected.
15+>>> from django.db.models import Q
16+>>> Reporter.objects.filter(Q(first_name='John')|Q(article__headline__startswith='Paul')).distinct()
17+[<Reporter: John Smith>, <Reporter: Paul Jones>]
18+>>> r3 = Reporter(first_name='Luke', last_name='Field', email='luke@example.com')
19+>>> r3.save()
20+>>> Reporter.objects.filter(Q(first_name='Luke')|Q(article__headline__startswith='Paul')).distinct()
21+[<Reporter: Luke Field>, <Reporter: Paul Jones>]
22+>>> r3.delete()
23+
24 # If you delete a reporter, his articles will be deleted.
25 >>> Article.objects.all()
26 [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>, <Article: This is a test>, <Article: This is a test>]