我有一个包含我的模型的 django 数据库(sqlite)。特别是,有一个模型的日期字段在数据库中不为空(通过 sqlite shell 确认),其转储数据始终序列化为空。
我在 python 2.7.5 下使用 django 1.4.5。
真正奇怪的部分是它并非对所有日期字段都这样做。这似乎是datetime
在数据库中键入的唯一字段,而不仅仅是date
.
我的问题是:是什么原因造成的,我该如何阻止它?或者,什么是转储数据的好选择?
这是模型代码:
class AccountTransaction(models.Model):
value = models.FloatField()
description = models.CharField(max_length = 1000)
credit_in = models.ForeignKey(Organisation, on_delete = models.PROTECT, related_name='credits_in')
debit_in = models.ForeignKey(Organisation, on_delete = models.PROTECT, related_name='debits_in')
date = models.DateTimeField(auto_now_add=True)
class Meta:
get_latest_by = 'date'
这是架构:
CREATE TABLE "mainapp_accounttransaction"
("credit_in_id" integer,
"description" varchar(1000),
"date" datetime NOT NULL DEFAULT '2012-06-18',
"debit_in_id" integer,
"id" integer PRIMARY KEY,
"value" real);
这是一个使用空日期错误序列化的实例的示例:
{
"pk": 1,
"model": "mainapp.accounttransaction",
"fields": {
"debit_in": 1,
"date": null,
"credit_in": 1,
"description": "Charge for standard of Example5-1 limited (order Incorporation Order no. 13 for Example5-1 limited (UK Company temporary number: UN177efa40-2022-11e1-b383-e89a8f7f9c14))",
"value": 100.0
}
}
为了比较,这是在数据库中查找的结果:
sqlite> select * from mainapp_accounttransaction where id is 1;
1|Charge for standard of Example5-1 limited (order Incorporation Order no. 13 for Example5-1 limited (UK Company temporary number: UN177efa40-2022-11e1-b383-e89a8f7f9c14))|2012-06-18|1|1|100.0
更新:这是一件有趣的事情:
>>> import mainapp.models
>>> ats = mainapp.models.AccountTransaction.objects.all()
>>> [o.date for o in ats]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]