Hi Ryan,
On 01/06/2016 08:36 PM, Ryan Causey wrote:
> I've been reading the Django docs, googling, and searching this mailing
> list on the proper configuration and usage of Django when USE_TZ = True.
> However, I'm still a bit fuzzy on things and would like clarification on
> the following:
>
> 1. The documentation says that when USE_TZ = True that "Django stores
> datetime information in UTC in the database, uses time-zone-aware
> datetime objects internally, and translates them to the end user’s
> time zone in templates and forms." Does this mean that the TIME_ZONE
> setting in settings.py should be UTC? Or should it be the timezone
> in which the database server is located?
Django will use UTC internally (by which I mean, return UTC from the
database, and return UTC from `django.utils.timezone.now`) regardless of
the TIME_ZONE setting. You can think of the TIME_ZONE setting as the
default "end-user timezone" for your application; it doesn't affect
internal storage of datetimes.
If you have users in different timezones and you store each user's
timezone (e.g. in a profile model or custom user model), you can call
`django.utils.timezone.activate` (e.g. in a middleware's
`process_request` method) to activate the current user's timezone, which
will automatically cause templates and forms to display datetimes in
their timezone. If you never call `timezone.activate`, templates and
forms will display datetimes in the timezone defined by the TIME_ZONE
setting.
So basically there are two types of applications: those with per-user
timezones, and those that assume all users are in the same timezone.
For the former, you should be calling `timezone.activate` on every
request, and it doesn't really matter what you set TIME_ZONE to.
For the latter, you should set TIME_ZONE to the time zone that you
assume all your users live in, or at least that all your users will be
OK seeing their datetimes represented in.
> 2. I am using a postgresql database, and the installation defaulted the
> database's timezone to my local one. In support of Django storing
> all datetime information in UTC in the database, does the database's
> timezone needs to be set to UTC? Or is it correct for the database's
> timezone to be the local one?
Much like Django, Postgres _always_ stores in UTC internally (see the
docs); the server timezone setting only affects what timezone you see
your timestamps in if you query them at the Postgres shell. So much like
Django's TIME_ZONE setting, the Postgres server timezone is for user
representation only, it doesn't affect internal storage.
Django always overrides the default Postgres server timezone with a
per-connection timezone of UTC when it connects, so really the Postgres
server timezone setting is irrelevant to a Django application. It'll
only make a difference if or when you connect to Postgres outside of Django.
> 3. The documentation also says that one should use the UTC timezone in
> the code, and only convert to local time when dealing with end
> users. I take this to mean that in any code I write I should set
> tzinfo = UTC for all datetime objects I create programmatically and
> let Django translate them in forms and views to the end user's
> timezone automatically. Is this correct?
This approach isn't strictly required, but Django (and I) strongly
recommend it. Working exclusively with UTC internally makes datetime
arithmetic straightforward. If you do datetime arithmetic with non-UTC
datetimes, you have to handle ambiguous and nonexistent datetimes around
DST transitions, which is a real pain. So yes: use UTC internally, and
let Django convert for you at the user-display boundary.
Carl