Django default=timezone.now + delta

前端 未结 1 1779
我在风中等你
我在风中等你 2021-01-07 16:50

Trying to set a timestamp for a key expiration in Django model and bumped into this issue :

My current code :

key_expires = models.DateTimeField(de         


        
相关标签:
1条回答
  • 2021-01-07 17:21

    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.)

    0 讨论(0)
提交回复
热议问题