Let's say I have two Django models Person and Company as follows: -
class Company(models.Model):
name = models.CharField()
class Person(models.Model):
last_name = models.CharField(blank=True)
first_name = models.CharField()
company = models.ForeignKey(Company, null=True, blank=True)
A Person may or may not belong to a Company.
I am using MySQL. I want all Persons that do not belong to any Company, that is, Persons where company is null.
If I do Person.objects.filter(company__isnull=True)
I get an SQL which is essentially: -
SELECT * FROM PersonTable LEFT OUTER JOIN AgencyTable ON (PersonTable.company_id = AgencyTable.id) WHERE AgencyTable.id IS NULL
How do I go about achieving the following SQL: -
SELECT * FROM PersonTable INNER JOIN AgencyTable ON (PersonTable.company_id = AgencyTable.id) WHERE AgencyTable.id IS NULL
From what I gather from reading up the Django Users mailing list, this used to be the behavior before QuerySet Refactor.
EDIT -- Now I see the blasphemy of my question!
What I want to say is I simply want to do
SELECT * FROM PersonTable WHERE PersonTable.company_id IS NULL
Well, this question is old, and soon the patch will be in Django. But for the brief meantime, the answer is in http://code.djangoproject.com/ticket/10790:
Workaround: Instead of
Person.objects.filter(company=None)
use
Person.objects.exclude(company__isnull=False)