Switching Django USE_TZ to True
I have a site which is pretty old (started at Django 1.6) but has been upgraded over time and is now at Django 4.2. It has always had USE_TZ=False
and TIME_ZONE='America/Chicago'
.
We have a lot of critical dates (subscriptions, purchase records, etc), many of which are fed from external webhooks from our payment processor.
I'm looking at adding another Django app, but it requires USE_TZ=True
. Anyone have any insight into what might happen if I turn it on? I've turned it on in my development environment and I don't see any obvious issues, but can't easily replicate the webhooks I receive and daily tasks that extensively use date calculations. Where should I be looking for issues?
I'm rather confused on what you're asking. What do you mean specifically by turn on? Turn on what?
If you want to switch from True
to False
or vice-versa for your USE_TZ
. You can go to your Django settings and change it to False
. But I believe it's True by default.
Not sure if this is the answer you're looking for, your question is kind of vague.
You can set USE_TZ = True
as it ensures consistency when handling datetimes across different regions which I guess is what you want.
When USE_TZ
is False
, Django will store all datetimes in your TIME_ZONE
setting. When USE_TZ
is True
, TIME_ZONE
setting becomes the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.
If you are using PostgreSQL, it converts datetimes from the connection’s time zone to UTC on storage, and from UTC to the connection’s time zone on retrieval. This means that you can switch between USE_TZ = False
and USE_TZ = True
freely. The database connection’s time zone will be set to TIME_ZONE
or UTC
respectively, so that Django obtains correct datetimes in all cases. You don’t need to perform any data conversions.
But other backends store datetimes without time zone information. If you switch from USE_TZ = False
to USE_TZ = True
, you must convert your data from local time to UTC – which isn’t deterministic if your local time has DST.
See Django 4.2 docs.