I have this code in my model:
added_time = models.DateTimeField(
default=datetime.datetime.now()
)
After I migrate and restart uwsgi, I get first datetime in MariaDB now, and all next - exactly the same as first after resetting uwsgi.
2015-04-19 16:01:46
2015-04-19 16:01:46
2015-04-19 16:01:46
2015-04-19 16:01:46
I fixed it by changing code to:
added_time = models.DateTimeField(
auto_now_add=True
)
Though I fixed the problem, I'm not really sure why there even was such behavior?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With