2

Got stuck at a point in Django.

I have stored a value in my PostgreSQL db in the

TIME_ZONE = 'Asia/Kolkata'

format, but when I fetch the same from Django, it gets converted into UTC.

Example:

I have a timeline field in my table in postgres with a stored value of 2015-05-02 05:29:59+05:30

When I fetch it from django shell it gives datetime.datetime(2015, 5, 1, 23, 59, 59, tzinfo=UTC)

Kindly assist. Do I need to change any settings in Django? My current settings are:

TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True

Apart from this , tzlocal is giving desired timezone value

>>> from tzlocal import get_localzone
>>> get_localzone()
<DstTzInfo 'Asia/Kolkata' LMT+5:53:00 STD>
Nirmal Vatsyayan
  • 384
  • 1
  • 5
  • 20
  • it is ' timestamp with time zone' in postgres . – Nirmal Vatsyayan Apr 13 '15 at 09:26
  • in select command output it as expected in 2015-05-02 05:29:59+05:30 format . – Nirmal Vatsyayan Apr 13 '15 at 09:28
  • in select command output it as expected in 2015-05-02 05:29:59+05:30 format . I am expecting same output when i purge the value from DB using django shell , current output is :>>> obj.timeline datetime.datetime(2015, 5, 1, 23, 59, 59, tzinfo=) – Nirmal Vatsyayan Apr 13 '15 at 09:35
  • select query in DB gives perfect result , issue comes when i fetch the data from db in django , it gets converted in UTC format only . – Nirmal Vatsyayan Apr 13 '15 at 09:38
  • output of select command in db is :greymeter2=# select timeline from myapp_test where id=6; timeline --------------------------- 2015-05-02 05:29:59+05:30 (1 row) – Nirmal Vatsyayan Apr 13 '15 at 09:40
  • if the information is relevant for your question then don't put it in the comments, [update your question instead](http://stackoverflow.com/posts/29601488/edit) – jfs Apr 13 '15 at 11:30
  • does `import pytz` work? What does `tzlocal.get_localzone()` return? What is postgres connection’s time zone? – jfs Apr 13 '15 at 11:35
  • yes it is working , edited my question and added the snippet . – Nirmal Vatsyayan Apr 13 '15 at 13:44
  • it looks like this issue: [Django postgres connection's timezone is always set to UTC. Breaks date_trunc function](https://code.djangoproject.com/ticket/23524) – jfs Apr 13 '15 at 19:22

1 Answers1

1

Per the docs:

When support for time zones is enabled, 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.

So it seems you're only going to get the correct timezone in your templates, and not in shell.

However, to see the date in your format, assuming the name of the field is date_created, you can do

from django.utils import timezone

print str(timezone.localtime(obj.date_created))

OR you could try these methods in the Usage docs

onyeka
  • 1,517
  • 13
  • 18
  • I have changed my DB settings in postgres to timezone = 'Asia/Calcutta' in file postgresql.conf , hence django is storing values in expected IST timezone only , are you saying django will always return in UTC format only , regardless of format in am storing data into ? also in my django settings timezone is TIME_ZONE = 'Asia/Kolkata' , kindly assist – Nirmal Vatsyayan Apr 13 '15 at 10:00
  • or is there any way i could purge data in which it is stored in DB ? – Nirmal Vatsyayan Apr 13 '15 at 10:02
  • I updated my answer, try wrapping the time field in a 'str()' – onyeka Apr 13 '15 at 10:32
  • 1
    it gives collected time in string format only i.e '2015-05-01 23:59:59+00:00' , my value is stored in DB in +5:30 timezone format but django converts it to UTC format when i fetch it from DB , all i want is to fetch data in +5:30 time format only . Example :: data in db is - 2015-05-02 05:29:59+05:30 but when i fetch it from django and typecast it to str it gives >>> str(obj.timeline) '2015-05-01 23:59:59+00:00' , hence see format changes to UTC , this is the issue – Nirmal Vatsyayan Apr 13 '15 at 13:43
  • @NirmalVatsyayan I've updated my answer again. Use the timezone function as shown. That should work. Also, going forward, try the suggestion [in this comment here](http://stackoverflow.com/questions/16037020/djangos-timezone-now-does-not-show-the-right-time#comment36505072_16037255) for getting this format by default – onyeka Apr 13 '15 at 15:22