views:

18

answers:

1

Working with Django docs' sample Blog and Entry models, how would one get a queryset of all Blog objects that have name = "a" and that are not associated with any instance of the Entry model?

In raw (My)SQL terms, what is the Django ORM equivalent of:

SELECT * FROM blog_table bt
WHERE bt.name='a' AND bt.id NOT IN (SELECT et.blog_id FROM entry_table et)
+2  A: 

What you need is a list of Blog instances that have name = "a" and do not have any associated entries. You can do this by:

Blog.objects.filter(name = "a", entry = None)
#                   ^^^^        ^^^^^
#           <Match name>        <Should have no associated Entry instances>
Manoj Govindan
That's pretty neat. Seems to work alright. Thanks.
chefsmart