【发布时间】:2013-08-14 12:33:48
【问题描述】:
我有一个模型 Person,它有时在 birth_date 字段中没有任何内容。这是一个例子:
(Pdb) dude = Person.objects.get(pk=20)
(Pdb) dude
<Person: Bob Mandene>
(Pdb) dude.birth_date
(Pdb) dude.birth_date == None
True
如何过滤这些具有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.
以下不返回id为20的记录。
Person.objects.filter(birth_date = "")
如何过滤为 None 的字段?看起来 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."]
型号
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'
【问题讨论】:
-
is_null仅在您在模型中设置null=True时才有效;那么模型是什么样的呢? -
用模型更新了问题。这是一个从 MsSQL 转移到 MySQL 的遗留表。
-
据说
is_null必须工作`。如果出生日期的值为空,则必须检查数据库。
标签: django django-models