Short way to serialize datetime with marshmallow

Alexey Lysenko picture Alexey Lysenko · Mar 4, 2016 · Viewed 14.5k times · Source

Here is my situation: I store some datetime in MSSQL, which i get in my python application via SQLAlchemy, and then serialize it thru Marshmallow like this:

class MyVisitSchema(Schema):
    cafe = fields.Nested(CafeSchema)
    started_at = fields.DateTime()
    ended_at = fields.DateTime()

    class Meta:
        additional = ('duration',)
        ordered = True

But here the problem: after serialization i get something like "started_at": "1994-05-20T00:00:00+00:00" which says that UTC+0, but i store all my dates in DB without any timezone info, but in UTC+3.

I know that i can use fields.Method() to change output timezone, but it looks inconvenient. Any ideas how to make my serializer work as it should?)

Answer

Alexey Lysenko picture Alexey Lysenko · Mar 4, 2016

Found some info in official documentaries. So, my issue can be solved using

started_at = fields.DateTime('%Y-%m-%dT%H:%M:%S+03:00')

hardcode a bit, but looks better than using additional function with fields.Method()