Django default=timezone.now + delta

大兔子大兔子 提交于 2019-12-04 22:18:46

default takes a callable, so you just need to write a function to do what you want and then provide that as the argument:

def one_day_hence():
    return timezone.now() + timezone.timedelta(days=1)

class MyModel(models.Model):
    ...
    key_expires = models.DateTimeField(default=one_day_hence)

(As discussed here, resist the temptation to make this a lambda.)

I'm working on django 2.1.7, In this version it doesn't required to write a function for default. You can simply use your by simple modification of your previous code, which is:

key_expires = models.DateTimeField(default=timezone.now() + timezone.timedelta(days=1))

If you notice the difference is () after timezone.now

You can follow the link for more information about timedelta

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