0

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()?

Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78

1 Answers1

2

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

erhesto
  • 1,176
  • 7
  • 20
  • This works! Thanks for the answer. Do we have to handle the exception just in case if the date is not properly stored in the database? – Ganesh Satpute Sep 07 '17 at 06:14
  • Hard to say. Personally, I probably wouldn't bother in case if I'd own both database and code (or try to catch exception somewhere else), but actually Marshmallow gives some list of library-oriented exceptions that you might find useful: http://marshmallow.readthedocs.io/en/latest/api_reference.html#module-marshmallow.exceptions – erhesto Sep 07 '17 at 07:59