Django filter for many to many by multiple

whitebear :

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

c.grey :

try this

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

hope this helps

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291557&siteId=1