Django default=datetime.now() in models always saves same datetime after uwsgi reset Django default=datetime.now() in models always saves same datetime after uwsgi reset django django

Django default=datetime.now() in models always saves same datetime after uwsgi reset


default=datetime.datetime.now() is evaluated at parsing/compile time of the model. It is not changed afterwards. To evaluate now() at the time of adding/updating an object, you have to use:

default=datetime.datetime.now, which sets now as the callable. Django will call it at runtime.

Your solution of using auto_now_add is of course also correct (yet semantically different -- passing a default will set the value every time the model is saved, whereas auto_now_add only does it once, at creation time).

Don't dispair, this is avery common mistake.


You need to pass datetime.datetime.now instead of datetime.datetime.now() to default. Otherwise, the default value is computed when the model is initialized hence you always get the same value after a restart.

See the Django documentation for a more thorough explanation.

If using Django's time zones support, remember to use django.utils.timezone.now instead of datetime.datetime.now.