Django: USE_TZ = False 不更新 Postgres 数据库设置

Django: USE_TZ = False is not updating Postgres database setting

所以我对 Django 中的 Use_Tz 有一些困惑。

刚开始项目的时候,USE_TZ设置为True,我们也把USE_I18N设置为True。

但是,上周决定将这些设置设置为 False。根据文档 here,Postgresql 应该能够根据 settings.py 文件自动在 True 和 False 之间切换。

但是,我表格中的日期时间字段在末尾显示时区戳 +01。

此外,当我使用命令 \d survey_server_app_activitylog 直接进入数据库时​​,我看到以下内容:

 date_reg       | timestamp with time zone |           

具有日期时间的所有字段显示相同的条目。但是,我认为他们应该设置为timestamp without time zone

那么,是什么原因呢?我完全错误地解释了 USE_TZ = False 吗?我需要手动设置 postgres 数据库中的列吗?

请参阅 section of the timezone documentation 专门处理 PostgreSQL:

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.

As a consequence, if you’re using PostgreSQL, you can switch between USE_TZ = False and USE_TZ = True freely. The database connection’s time zone will be set to TIME_ZONE or UTC respectively, so that Django obtains correct datetimes in all cases. You don’t need to perform any data conversions.

这回答了您关于列类型为何未更改的具体问题。目前尚不清楚这实际上给您带来了什么问题,因此很难提供进一步的建议。如果您尝试在 Django 之外更改列的类型,我不会期望正确的行为。