Django filter for many to many by multiple

Viewed 39

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??

1 Answers

try this

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

hope this helps

Related