whitebear
whitebear

Reputation: 12445

Django filter for many to many by multiple

Author has the manytomany entity Article

I can use filter for manytomany like this

a = Author.objects.get(id=1)

Article.objects.filter(authors=a)

However I want to filter auther a and auther b

like

Article.objects.filter(authors=a and authors=b)

How can I make it??

Upvotes: 2

Views: 41

Answers (1)

rahul.m
rahul.m

Reputation: 5854

try this

# for OR
from django.db.models import Q
Article.objects.filter(Q(authors=a) | Q(authors=b))

hope this helps

Upvotes: 3

Related Questions