How to deserialize datetime from json using MongoEngine

239 views Asked by At

I'm trying to deserialize a datetime from a JSON string into a pendumul datetime object using MongoEngine. Consider the following code

#!/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

even after declaring Event.datetime as a DateTimeField, type(datetime) returns string and the assertion fails. How can I specify a custom deserializer to mongoengine?

0

There are 0 answers