Python Marshmallow - Customize formatting of class member

[亡魂溺海] 提交于 2019-12-12 04:54:24

问题


I have Marshmallow serializer written as

class TestSchema(Schema):
    created_date = fields.Str(required=False)


class Test(database.Model):
       created_date = database.Column(database.DateTime,
                                      default=datetime.utcnow,
                                      nullable=False)


testSchema = TestSchema()
testSchema.dump(new Test())

Is there any way I can change the output of created_date using created_date.isoformat()?


回答1:


Use fields.DateTime marshmallow field for SQLAlchemy datetime object instead of fields.Str. In fact, iso format is default format, but you can specify other in format parameter. Docs: here



来源:https://stackoverflow.com/questions/46074589/python-marshmallow-customize-formatting-of-class-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!