1

I am using Django 1.8.6. Datetimes are stored inside a PostgreSQL database in UTC. Below are my project-wide settings:

USE_TZ = True
TIMEZONE = 'UTC'
USE_L10N = True

I wonder if it is possible for me to show the time in a specified timezone inside the Django admin? (I am the only user of the admin.) Thanks.

Jonas
  • 534
  • 8
  • 16
  • What exactly do you want? 1) for some object datetime field; 2) in admin sidebar; ... – madzohan Nov 08 '15 at 15:26
  • I want the datetime field of a model to be displayed in local time in admim but stored in database as UTC. – Jonas Nov 08 '15 at 15:27

1 Answers1

1

for example your model

class SomeModel(models.Model):
    datetime_filed = models.DateTimeField(default=timezone.now)
  1. Simplest solution is change settings.TIMEZONE to your local one (one of pytz.all_timezones) django will store datetime_filed with tzinfo=<UTC> but in all templates by default it will be localized ... in your apps templates you can use one of these template tags https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#template-tags

...

madzohan
  • 11,488
  • 9
  • 40
  • 67
  • Thx. What if I want everthing stored as UTC in db? In that case I have to set TIMEZONE to UTC. – Jonas Nov 08 '15 at 16:23
  • the PostgreSQL backend stores datetimes as timestamp with time zone. In practice, this means it converts datetimes from the connection’s time zone to UTC on storage, and from UTC to the connection’s time zone on retrieval. https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#postgresql – madzohan Nov 08 '15 at 16:27
  • So it means that PostgreSQL stores time as the epoch time in seconds along with the time zone the user is located in. And this is the same no matter what settings.TIMEZONE is? settings.TIMEZONE does not affect how time is stored in psql, it affects presentation? – Jonas Nov 08 '15 at 16:33
  • @madzohanuse use timezone.now instead of timezone.now(), otherwise it will set a fixed date – wasabigeek Jul 13 '16 at 15:19
  • 1
    @wasabigeek yep) but it was just for example and your review is point for another question ... anyway I've changed it ... good explanation is here http://stackoverflow.com/a/13226368/3033586 – madzohan Jul 13 '16 at 19:08