I have read the Django
Many to Many documentation.
Based on the example, I am stuck on how to formulate the following queries (this related to my "real life" problem):
- What Publications have no Articles?
- What Publications have at least one Article?
Doing it with a loop is straightforward:
publications_with_no_articles = [publication for publication in Publication.objects.all() if not publication.article_set.all().exists()]
publications_with_some_articles = [publication for publication in Publication.objects.all() if publication.article_set.all().exists()]
But, that's slow if you have a lot of publications - a query for each one.
Is there a way to formulate the queries with no loop, returning a QuerySet?
Apologies in advance if I am missing something simple...