You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since datetime.datetime.strftime doesn't have an easy way to convert a datetime with microsecond precision to millisecond precision this also leaks into Marshmallow.
I have come up with
from marshmallow import fields
from datetime import datetime
from marshmallow import Schema
class Blah(Schema):
date_time = fields.Function(
lambda dictionary: dictionary["date_time"].strftime("%FT%T.%f")[:-3] + "Z"
)
x = datetime.utcnow()
print(f"{x=}") # prints x=datetime.datetime(2022, 8, 9, 16, 38, 44, 165105)
Blah().dumps({"date_time":x}) # prints '{"date_time": "2022-08-09T16:38:44.165Z"}'
Is there a cleaner way to do this?
I also dislike the fact that this is now linked to the input type, if I now use a class to dump the data from instead of a dict I will have to change the mapping to be
Since
datetime.datetime.strftime
doesn't have an easy way to convert a datetime with microsecond precision to millisecond precision this also leaks into Marshmallow.I have come up with
Is there a cleaner way to do this?
I also dislike the fact that this is now linked to the input type, if I now use a class to dump the data from instead of a dict I will have to change the mapping to be
I feel I'm missing something
The text was updated successfully, but these errors were encountered: