Django模型的DateTimeField使用UTC,即使时区是亚洲/加尔各答

2024-05-20 20:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Django 1.11。 我的settings.py配置了以下内容:

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

这是印度标准时间。我的系统设置为IST。MySQLcurrent_time返回IST,Django的服务器在运行时,在IST中显示时间戳。Python的datetime.now()也给出了IST。在

现在的问题是,我的models.py有以下字段:

^{pr2}$

数据库是MySQL。当我在其中插入任何记录时,时间戳以UTC为单位。我正考虑添加一些中间件,正如thisanswer建议的那样,但这并不是推荐的方式。为什么即使在配置了所有IST时也要使用UTC?我是否缺少一些需要为模型单独设置的配置?在

请注意与我要求的不一样。我想改变整个时区,这样它对模型也有效。在


Tags: djangopytruezonesettingstimeuse时间
1条回答
网友
1楼 · 发布于 2024-05-20 20:21:20

如果USE_TZTrue,则datetimesalways be stored in UTC。在

唯一的办法是将USE_TZ设置为Falseauto_nowusesdjango.utils.timezone.now(),其中{a3}如下:

If USE_TZ is False, this will be a naive datetime (i.e. a datetime without an associated timezone) that represents the current time in the system’s local timezone.

听起来像你要的。在

但你确定这就是你想要的吗?由于Asia/Calcuttahad DST in the past,因此有时您无法在数据库中明确表示。这就是为什么UTC是存储日期时间的标准选择。在

相关问题 更多 >