You do not have to convert it to UTC before saving it as Django will automatically do that for you. When you retrieve it, it will be retrieved as the timezone that was defined in the TIMEZONE
setting.
You can override that with activate()
and to see the current timezone, use django.utils.timezone.get_current_timezone()
.
This means that the time you retrieve from the database may be different than the timezone of the datetime object you used to save the object originally. However, it will still be the same moment in time.
If you have a timezone for each user, you should save this with the user's profile and call activate()
with the user's timezone BEFORE retrieving the model instance that contains the DateTimeField
. The retrieved object will be in the appropriate timezone.
Alternatively you can use the override()
context manager.
If you are sure you always want the datetime
objects in the user's timezone instead of the server's timezone, write a custom middleware to call activate()
on request.user
. You will need to store/retrieve the timezone yourself as Django has no way of knowing what the user's timezone is and does not store it in the User model by default.