Short way to serialize datetime with marshmallow
If it does not work, use the keyword format
started_at = fields.DateTime(format='%Y-%m-%dT%H:%M:%S+03:00')
I would rather use datetimeformat
, see: https://marshmallow.readthedocs.io/en/3.0/api_reference.html
Example:
class MyVisitSchema(Schema):
cafe = fields.Nested(CafeSchema)
started_at = fields.DateTime()
ended_at = fields.DateTime()
class Meta:
additional = ('duration',)
ordered = True
# dateformat = '%Y-%m-%dT%H:%M:%S%z'
dateformat = '%Y-%m-%dT%H:%M:%S+03:00'
And I prefer to:
class BaseSchema(Schema):
class Meta:
dateformat = '%Y-%m-%dT%H:%M:%S+03:00'
class MyVisitSchema(BaseSchema):
cafe = fields.Nested(CafeSchema)
started_at = fields.DateTime()
ended_at = fields.DateTime()
class Meta(BaseSchema.Meta):
additional = ('duration',)
ordered = True
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()