[英]django's DateField model field and acceptable values
I'm having a bit of trouble with django's DateField model field. 我在Django的DateField模型字段中遇到了麻烦。 Shouldn't it be able to accept fiveDaysLater
as a valid date object? 它不应该可以接受fiveDaysLater
作为有效的日期对象吗? When I try to add fiveDaysLater into the database, I get an error saying cannot add null value to date
. 当我尝试将FiveDaysLater添加到数据库中时,出现一条错误消息,提示cannot add null value to date
。 However, the second I change the date
field to a regular CharField
, the fiveDaysLater value is added to the database with no problem. 但是,第二次我将date
字段更改为常规CharField
,将FiveDaysLater值添加到数据库中没有问题。 fyi if I print fiveDaysLater, I get 2011-09-28
如果我打印出fiveDays以后,我就得到了2011-09-28
My view: 我的观点:
def myView():
now = datetime.date.today()
fiveDaysLater = now + datetime.timedelta(days=5)
newDate = Speech(date = fiveDaysLater)
newDate.save()
My model 我的模特
class Speech(models.Model):
date = models.DateField()
"However, the second I change the date field to a regular CharField..." Just a suspicion but if you made this change in your code, make sure to delete and recreated the Speech table using syncdb, otherwise, sqlite will not be aware of this change. “不过,第二次我将日期字段更改为常规的CharField ...”只是怀疑,但是如果您在代码中进行了此更改,请确保使用syncdb删除并重新创建Speech表,否则,sqlite不会知道这个变化。 (or you could change the datatype using sqlite exporer for firefox or something like that...) (或者您可以使用sqlite exporer更改Firefox或类似的数据类型...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.