在django字段上过滤无

Tre*_*ewq 5 django django-models

我有一个模特人,有时在该birth_date领域没有任何东西.这是一个例子:

(Pdb) dude = Person.objects.get(pk=20)
(Pdb) dude
<Person: Bob Mandene>
(Pdb) dude.birth_date
(Pdb) dude.birth_date == None
True
Run Code Online (Sandbox Code Playgroud)

如何过滤这些记录birth_date == None

我已经尝试了以下但没有成功:

1:"birth_date__isnull"不起作用.

 Person.objects.filter(birth_date__isnull = True) 
 # does not return the required 
 # object.    Returns a record that have birth_date set to 
 # NULL when the table was observed in MySQL.
Run Code Online (Sandbox Code Playgroud)

以下内容不会返回ID为20的记录.

Person.objects.filter(birth_date = "")
Run Code Online (Sandbox Code Playgroud)

如何过滤无字段?它似乎是NULL而None是不同的.当我使用sequel pro(mysql图形客户端)看到数据时,我看到"0000-00-00 00:00:00",以下内容也无效

(Pdb) ab=Patient.objects.filter(birth_date = "0000-00-00 00:00:00")
 ValidationError: [u"'0000-00-00 00:00:00' value has the correct format 
 (YYYY-MM-DD     HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time."]
Run Code Online (Sandbox Code Playgroud)

模型

class Person(models.Model):
    person_no = models.IntegerField(primary_key=True)
    locationid = models.IntegerField(null=True, db_column='LocationID', blank=True) 
    address =  models.ForeignKey('Address', db_column = 'address_no')
    birth_date = models.DateTimeField(null=True, blank=True)
    class Meta:
       managed = False
       db_table = 'person'
Run Code Online (Sandbox Code Playgroud)

sch*_*ngt 0

mysql 中的 NULL 在 python 中变成 None ,所以你用birth_date__isnull=True 得到的应该返回那些birth_date 为 None 的值。