Reputation: 110452
I am trying to do the following query:
SELECT * FROM catalog WHERE master_id IS NULL
The master
field is a ForeignKey, but in this case, I just need to know whether it is NULL or not so I don't need to do a foreign key lookup. However, if I do:
Catalog.objects.filter(master_id=None).values_list('is_tv', flat=True).count()
Here is the example statement it does:
SELECT ••• FROM `catalog` LEFT OUTER JOIN `main_titlemaster`
ON (`main_catalog`.`master_id` = `main_titlemaster`.`id`)
WHERE `main_titlemaster`.`id` IS NULL
How can I prevent this useless FK lookup without diving into raw SQL?
Upvotes: 1
Views: 90
Reputation: 52203
You should use __isnull
lookup keyword.
Catalog.objects.filter(master__isnull=True).values_list('is_tv', flat=True).count()
Upvotes: 3