Python Marshmallow - Customize formatting of class

2019-08-18 06:42发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-08-18 07:11

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

查看更多
登录 后发表回答