wengole
wengole

Reputation: 63

In Django filtering on __isnull seems to result in LIKE 'None'

I have a model with the following field

class Callstat(models.Model):
agentdisplayname = models.CharField(null=True, max_length=60, db_column='AgentDisplayName', blank=True)

class Meta:
    db_table = u'callstat'

def __unicode__(self):
    return u'%d: %s' % (self.callid, self.telnum)

However when I do the following,

call = Callstat.objects.all().filter(agentdisplayname__isnull=True)

MySQL's general_log shows the resulting SQL as

SELECT `callstat`.`AgentDisplayName` FROM `callstat` WHERE `callstat`.`AgentDisplayName` LIKE 'None';

The data in the database is added by a separate application and I have no control over the fact that it is leaving fields as NULL. I'm fairly certain this can't be a bug in Django so any help as to what I am doing wrong would be greatly appreciated.

EDIT: OK turns out I'd missed something further down in my code, and this filter had been overwritten by a __exact filter in a loop.

Upvotes: 2

Views: 405

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31633

Set null=True on your column. By default null parameter it is set to false, which means NULL is not a permitted value.

agentdisplayname = models.CharField(null=True, max_length=60, db_column='AgentDisplayName', blank=True, null=True)

Probably you would need to change the existing db column as well.

Upvotes: 2

Related Questions