I have read the Django Many to Many documentation 
<https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/>.

Based on the example, I am stuck on how to formulate the following queries 
(this related to my "real life" problem):

   1. What Publications have no Articles?
   2. 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...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b792c9ba-08c4-42f7-a8a2-feaf8cdb666d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to