我正在尝试datetime
使用 MongoEngine 将日期时间从 JSON 字符串反序列化为 pendumul 对象。考虑以下代码
#!/usr/bin/env python3
import pendulum as pd
import mongoengine as db
TZ_EST = pd.timezone('America/New_York')
class Event(db.Document):
datetime = db.DateTimeField(required=True)
json = """
{
"datetime": "2021-04-11T10:46:47-05:00"
}
"""
event = Event.from_json(json)
datetime = pd.datetime(2021, 4, 11, 10, 46, 47, 0, TZ_EST)
assert event.datetime == datetime
即使在声明Event.datetime
为 a之后DateTimeField
,也会type(datetime)
返回字符串并且断言失败。如何为 mongoengine 指定自定义反序列化器?