Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django timezone settings change does not affect model

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?

like image 774
SaidbakR Avatar asked Feb 11 '23 07:02

SaidbakR


1 Answers

From the documentation:

Note that now() will always return times in UTC regardless of the value of TIME_ZONE; you can use localtime() 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.

like image 82
Kevin Christopher Henry Avatar answered Feb 13 '23 23:02

Kevin Christopher Henry