【发布时间】:2011-12-27 10:34:48
【问题描述】:
我认为 Django 创建了与时区无关的日期时间列,但是当我查看我的 Postgres 表时,我发现那里记录的值具有时区信息。
进一步我发现 Postgres 后端指示 Django 创建使用时区的列。
来自 django/db/backends/postgresql/creation.py:
data_types = {
...
'DateTimeField': 'timestamp with time zone',
...
架构显示创建的列被指定为“timestamp with time zone”。
CREATE TABLE notification_notice
(
...
created timestamp with time zone NOT NULL,
...
Postgres 日志显示已发送的更新语句。 Django 根据我的 Django 设置文件构建了一个使用 UTC 作为时区的 SQL 语句。
UPDATE "notification_notice" SET "sender_id" = 1, "group_id" = NULL, "notice_type_id" = 1, "content_type_id" = 21, "object_id" = 3, "created" = E'2011-11-11 22:31:08.022148' WHERE "notification_notice"."id" = 14
这就是我的桌子的样子。创建的列有一个时间戳,其时区为“-08”。 Postgres 必须检查我的系统时钟的时区才能找到时区。
my_db=# select * from notification_notice limit 1;
id | sender_id | group_id | notice_type_id | content_type_id | object_id | created | last_interaction_time
----+-----------+----------+----------------+-----------------+-----------+------------------------------+-----------------------
1 | | 3 | 21 | 53 | 6 | 2011-11-11 14:31:02.98882-08 |
(1 row)
问题:
Django 对时区没有放手政策吗?
为什么 Postgres 后端对 models.DateTimeField 使用时区?这是 Postgres 要求的吗?
有没有办法强制 Django 在不使用时区的 Postgres 中创建时间戳列?
【问题讨论】:
-
可能的答案:here
标签: python django postgresql datetime timezone