Created
August 27, 2019 20:12
-
-
Save KristobalJunta/796aa89725cd5d1b2fd630382cd686e0 to your computer and use it in GitHub Desktop.
A dict-serializable (JSON compatible) mixin for SQLAlchemy models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class JSONSerializable: | |
def _serialize(self, value): | |
if type(value) not in (int, float, bool, type(None)): | |
return str(value) | |
return value | |
def as_dict(self): | |
return {c.name:self._serialize(getattr(self, c.name)) for c in self.__table__.columns} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment