Overriding Django auto_now in datefiled

℡╲_俬逩灬. 提交于 2020-02-07 05:13:26

问题


Is there a way to pass a date to datafield that overrides auto_now? I want to only use auto_now if a date is not passed.


回答1:


According to the docs:

Note that the current date is always used; it’s not just a default value that you can override. https://docs.djangoproject.com/en/2.0/ref/models/fields/#datefield

So just don't use auto_now, use default, for example:

from django.utils import timezone

class YourModel(models.Model):
    date_approved = models.DateField(default=timezone.now)


来源:https://stackoverflow.com/questions/50191848/overriding-django-auto-now-in-datefiled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!