در رابطه های ManyToMany یا OneToMany استفاده کردن از spanning داخل filter به model دیگر این سوال رو ایجاد میکنه که هر دو شرط باید روی یک آبجکت اجرا شه یا هر کدام از شرط ها جدا؟ کد زیر توی مستندات جنگو روشن این موضوع رو نشون داده:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
>>> from datetime import date >>> beatles = Blog.objects.create(name='Beatles Blog') >>> pop = Blog.objects.create(name='Pop Music Blog') >>> Entry.objects.create( ... blog=beatles, ... headline='New Lennon Biography', ... pub_date=date(2008, 6, 1), ... ) <Entry: New Lennon Biography> >>> Entry.objects.create( ... blog=beatles, ... headline='New Lennon Biography in Paperback', ... pub_date=date(2009, 6, 1), ... ) <Entry: New Lennon Biography in Paperback> >>> Entry.objects.create( ... blog=pop, ... headline='Best Albums of 2008', ... pub_date=date(2008, 12, 15), ... ) <Entry: Best Albums of 2008> >>> Entry.objects.create( ... blog=pop, ... headline='Lennon Would Have Loved Hip Hop', ... pub_date=date(2020, 4, 1), ... ) <Entry: Lennon Would Have Loved Hip Hop> >>> Blog.objects.filter( ... entry__headline__contains='Lennon', ... entry__pub_date__year=2008, ... ) <QuerySet [<Blog: Beatles Blog>]> >>> Blog.objects.filter( ... entry__headline__contains='Lennon', ... ).filter( ... entry__pub_date__year=2008, ... ) <QuerySet [<Blog: Beatles Blog>, <Blog: Beatles Blog>, <Blog: Pop Music Blog]> |