Skip to content

Instantly share code, notes, and snippets.

@KristobalJunta
Created August 27, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KristobalJunta/796aa89725cd5d1b2fd630382cd686e0 to your computer and use it in GitHub Desktop.
Save KristobalJunta/796aa89725cd5d1b2fd630382cd686e0 to your computer and use it in GitHub Desktop.
A dict-serializable (JSON compatible) mixin for SQLAlchemy models
# -*- 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