8
Edit: Okay, I didn’t have a ManyToManyField to test on before so I was guessing. New code!
books = Book.objects.filter(title__contains="T")
categories = Category.objects.filter(book__in=books).distinct()
4
You need to filter by the ‘book’ field:
book_ids = list(Book.objects.filter(...).values_list('id', flat=True)
categories_queryset = Category.objects.filter(book__in=book_ids).distinct()
- [Django]-Django: How do you access a model's instance from inside a manager?
- [Django]-In Django, how to rename user model?
- [Django]-Django deep serialization – follow reverse foreign key constraints
Source:stackexchange.com