Django default=timezone.now() saves records using “old” time

前端 未结 2 1696
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 18:48

This issue has been occurring on and off for a few weeks now, and it\'s unlike any that has come up with my project.

Two of the models that are used have a timestam

相关标签:
2条回答
  • 2020-12-18 19:30

    Just set the parameter auto_now_add like this.

    timestamp = models.DateTimeField(auto_now_add=True)
    

    Update:

    Please don't use auto_now_add. It is not the recommended way, instead do this:

    from django.utils import timezone
    
    timestamp = models.DateTimeField(default=timezone.now)
    
    0 讨论(0)
  • 2020-12-18 19:35

    Just ran into this last week for a field that had default=date.today(). If you remove the parentheses (in this case, try default=timezone.now) then you're passing a callable to the model and it will be called each time a new instance is saved. With the parentheses, it's only being called once when models.py loads.

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