简体   繁体   English

django的DateField模型字段和可接受的值

[英]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.

相关问题 Django - 将字段注释到 model 查询,该查询将日期字段值添加到视图中的现有 model 字段 - Django - annotating a field to a model query which adds a datefield value to an existing model field in views Django 中的真正动态 DateField 值 - Truly dynamic DateField values in Django 改变了 Django 模型。 我为 DateField 提供了一次性默认值 = datetime.date。 发现无效。 删除了字段,仍然无法迁移 - Altered Django model. I provided a one-off default = datetime.date for DateField. Found that it's invalid. Removed the field and still can't migrate 如何确定Django模型字段中不同值的计数? - How to determine the count of distinct values in a Django model's field? Django model:将“星期几”字段设置为现有模型的字符串。DateField() - Django model: set a "week of day" field as string from existing models.DateField() 在Django模型中添加DateField后迁移时出错 - Error in migrate after adding DateField in Django Model Django 模型 DateField 属性限制选择 - Django model DateField attribute limit choices 如何在Django模型中为DateField设置空值? - How to set empty value to DateField in django model? Django model DateField格式为modelformset_factory - Django model DateField format in modelformset_factory Django:DateField“此字段不能为空。” - Django: DateField “This field cannot be blank.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM