Django 不 returns 存储格式的日期时间值

Django doesnot returns datetime value in stored format

在 Django 中卡住了。

我在

的 PostgreSQL 数据库中存储了一个值
TIME_ZONE = 'Asia/Kolkata'

格式,但是当我从 Django 获取相同格式时,它会转换为 UTC。

示例:

我在 postgres 中的 table 中有一个时间轴字段,其存储值为 2015-05-02 05:29:59+05:30

当我从 django shell 获取它时,它给出 datetime.datetime(2015, 5, 1, 23, 59, 59, tzinfo=UTC)

请提供帮助。我需要更改 Django 中的任何设置吗?我当前的设置是:

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

除此之外,tzlocal 还提供了所需的时区值

>>> from tzlocal import get_localzone
>>> get_localzone()
<DstTzInfo 'Asia/Kolkata' LMT+5:53:00 STD>

根据 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.

看来您只能在模板中获得正确的时区,而不是 shell。

但是,要以您的格式查看日期,假设字段名称是 date_created,您可以这样做

from django.utils import timezone

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

或者您可以在 Usage docs

中尝试这些方法