Django DateField default options

后端 未结 6 1146
醉酒成梦
醉酒成梦 2020-11-30 22:50

I have a model which has a date time field:

date = models.DateField(_(\"Date\"), default=datetime.now())
6条回答
  •  醉酒成梦
    2020-11-30 23:08

    This is why you should always import the base datetime module: import datetime, rather than the datetime class within that module: from datetime import datetime.

    The other mistake you have made is to actually call the function in the default, with the (). This means that all models will get the date at the time the class is first defined - so if your server stays up for days or weeks without restarting Apache, all elements will get same the initial date.

    So the field should be:

    import datetime
    date = models.DateField(_("Date"), default=datetime.date.today)
    

提交回复
热议问题