I want to make a sophisticated filter using Django ORM.
Model:
class Book(models.Model): title = models.TextField() bestseller = models.BooleanField(default=False) class Author(models.Model): name = models.TextField() books = models.ManytoManyField(Book)
How can I request all authors who have at least one best-selling book?
Query:
best_authors = Author.objects.filter(<relevant filter>)
Edit:
In accordance with the documentation , the following should work:
best_authors = Author.objects.filter(books__bestseller=True)
Unfortunately, this leads to the return of duplicate copyrighted objects (the same author for each best-selling book of his / her, again and again).
python django django-orm
Bradford wade
source share