Django DateField default options

后端 未结 6 1136
醉酒成梦
醉酒成梦 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:31

    Your mistake is using the datetime module instead of the date module. You meant to do this:

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

    If you only want to capture the current date the proper way to handle this is to use the auto_now_add parameter:

    date = models.DateField(_("Date"), auto_now_add=True)
    

    However, the modelfield docs clearly state that auto_now_add and auto_now will always use the current date and are not a default value that you can override.

提交回复
热议问题