0
我有一個包含我的模型的django數據庫(sqlite)。特別是,有一個模型的日期字段在數據庫中不是空的(用sqlite shell確認),該dumpdata總是序列化爲空。Django dumpdata將datetimes寫入空值
我在python 2.7.5下使用django 1.4.5。
真的很奇怪的部分是,它不會對所有日期字段執行此操作。這似乎是在數據庫中鍵入爲datetime
的唯一字段,而不僅僅是date
。
我的問題是:是什麼原因造成的,我該如何阻止它?或者,對於dumpdata有什麼好的選擇?
這裏的型號代碼:
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]
你能不能簡單地把模型改成'date = models.DateTimeField(auto_now_add = True,default ='2012-06-08')'? – karthikr
@ karthikr我可以,但沒有理由相信這會解決問題。問題不在於數據庫中的值爲空。它也會做錯事,因爲我實際上不希望這個日期成爲默認值。 – Marcin