首页 > 解决方案 > Django 和 PosgresSQL 在每次保存时添加一个时区偏移量

问题描述

我的 django 和 postgresql 在每次保存时将时区偏移添加到日期时间字段。它在 sqlite 上正常工作。

>>> from picture.models import *
>>> p=Picture.objects.get(id=561)
>>> p.date_taken
datetime.datetime(2020, 8, 12, 19, 23, tzinfo=<UTC>)
>>> p.save()
>>> p=Picture.objects.get(id=561)
>>> p.date_taken
datetime.datetime(2020, 8, 12, 21, 23, tzinfo=<UTC>)

我在 django 设置中尝试了各种配置组合:

USE_TZ = True
TIME_ZONE = 'UTC'

postgresql.conf

timezone = 'Etc/UTC'

psql

ALTER ROLE albumusertest SET timezone TO 'UTC';

一些片段:来自 postgres

   date_taken    | timestamp with time zone |           | not null | 

django

   date_taken = models.DateTimeField()

postgres 版本

psql (PostgreSQL) 11.12 (Debian 11.12-0+deb10u1)

django 3.2版

我真的很茫然。谁能帮我?

标签: djangopostgresqltimezone

解决方案


推荐阅读