I've got a model called Vote
with a field date
:
date = models.DateTimeField(auto_now_add=True)
When I add an element, the date in MySQL is an UTC date but I live in UTC+2 timezone
I think I correctly set the timezone in settings.py
:
TIME_ZONE = 'Europe/Paris'
Python use the right timezone :
>>> print datetime.datetime.now()
2013-07-03 09:05:04.474000
MySQL too :
> SELECT NOW( )
2013-07-03 09:00:48
I could set the date attribute manualy, it works but I would like to know why auto_now_add return a wrong date although python and mysql use the right timezone
Thank you
It's a complex binding to explain. From Django 1.4,
this refers to
TIME_ZONE
. So what is yourUSE_TZ
? If yourUSE_TZ
is True then Django will store datetime in UTC and useTIME_ZONE
to display in templates and interpret forms.This is because, if you change your
TIME_ZONE
later when hosting your site in another territory, it's easy to convert any datetimes from UTC to any timezones given.In Django 1.3 and earlier,
But doesn't tell you in what timezone the datetime will be stored in database. Need to experiment anyway(my guess is UTC).
print datetime.datetime.now()
prints the datatime according to the timezone setup of your server machine unless you have opened the python console viamanage.py shell
.Same goes for MySQL console. It shows the datetime in your machine timezone rather than what's stored in database if I'm right.