从 Flask-SQLAlchemy 获取 UTC 中的 DateTime 字段



我正在尝试使用Flask-SQLAlchemy将我的DateTime值恢复为UTC时区的代码。

我检查了我的Postgresql DB时区

> show timezone;
UTC

我尝试了这种方法:https://stackoverflow.com/a/26106482/169691(它不起作用(

class PGSQLAlchemy(SQLAlchemy):
def apply_driver_hacks(self, app, info, options):
options.update({
'echo': True,
'connect_args': {
"options": "-c timezone=utc",
}
})
super(PGSQLAlchemy, self).apply_driver_hacks(app, info, options)

db = PGSQLAlchemy()

有什么建议吗?

遇到了类似的问题。

来自 SQLAlchemy 文档:

或者对于基本的字符串和整数参数,通常可以在 URL 的查询字符串中指定它们:

e = create_engine("mysql://scott:tiger@localhost/test?encoding=utf8"(

connect_args.options了一遍,最后得到了

SQLALCHEMY_DATABASE_URI = 'postgres://{}:{}@{}:{}/{}?options=-c+timezone%3Dutc'.format(
PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME
)

为我修复了它。希望这有帮助。

最新更新