这是我的情况:我在 MSSQL 中存储了一些日期时间,我通过 SQLAlchemy 在我的 python 应用程序中获取了它,然后像这样通过 Marshmallow 对其进行序列化:
class MyVisitSchema(Schema):
cafe = fields.Nested(CafeSchema)
started_at = fields.DateTime()
ended_at = fields.DateTime()
class Meta:
additional = ('duration',)
ordered = True
但这里的问题是:在序列化之后,我得到类似 "started_at": "1994-05-20T00:00:00+00:00"
的内容,它表示 UTC+0,但我存储我在数据库中的所有日期都没有任何时区信息,但在 UTC+3 中。
我知道我可以使用 fields.Method()
来更改输出时区,但它看起来很不方便。有什么想法可以让我的序列化器正常工作吗?)
最佳答案
在官方纪录片中找到了一些信息。所以,我的问题可以用
解决started_at = fields.DateTime('%Y-%m-%dT%H:%M:%S+03:00')
有点硬编码,但看起来比使用 fields.Method()
的附加函数更好
关于python - 用棉花糖序列化日期时间的简短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35795622/