I have django 1.7.x project. I inserted new data via the shell as follows:
from polls.models import Question, Choice
from django.utils import timezone
q = Question(question_text = "What's new?", pub_date = timezone.now(), status = True)
q.save()
q.pub_date
However, I found that dates are set to UTC. Hence, in the project's settings I have changed timezone to be TIME_ZONE = 'Africa/Cairo'
. However, updating a record from the project's shell via manage shell
by q.pub_date = timezone.now()
does not change the timezone of the record to the new one. It is less than my development's computer by two hours, the difference between Cairo timezone and UTC. What could I missed it?
From the documentation:
Note that
now()
will always return times in UTC regardless of the value ofTIME_ZONE
; you can uselocaltime()
to convert to a time in the current time zone.
But note that Django always stores datetimes in UTC, so even if you convert it using localtime()
it won't affect how the value is stored. So q.pub_date
will either be in UTC or in a time zone defined in the database (not Django) settings.
But what matters is how the datetimes are presented to the user, and the way to control that is described in the timezone documentation.
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