Ticket #2080: m2o_or_lookup_test.diff

File m2o_or_lookup_test.diff, 1.3 KB (added by Antti Kaihola, 18 years ago)

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

  • modeltests/many_to_one/models.py

     
    217217>>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
    218218[<Reporter: John Smith>]
    219219
     220# OR lookups across foreign keys
     221#
     222# At revision 3077, the last test case fails to find Luke.  An inner
     223# join is created, and since Luke has no articles, he doesn't match
     224# the criteria.  For someone not familiar with how the ORM works, it's
     225# quite unexpected.
     226>>> from django.db.models import Q
     227>>> Reporter.objects.filter(Q(first_name='John')|Q(article__headline__startswith='Paul')).distinct()
     228[<Reporter: John Smith>, <Reporter: Paul Jones>]
     229>>> r3 = Reporter(first_name='Luke', last_name='Field', email='luke@example.com')
     230>>> r3.save()
     231>>> Reporter.objects.filter(Q(first_name='Luke')|Q(article__headline__startswith='Paul')).distinct()
     232[<Reporter: Luke Field>, <Reporter: Paul Jones>]
     233>>> r3.delete()
     234
    220235# If you delete a reporter, his articles will be deleted.
    221236>>> Article.objects.all()
    222237[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>, <Article: This is a test>, <Article: This is a test>]
Back to Top