Skip to content

Instantly share code, notes, and snippets.

@kevgathuku
Forked from kijun/sqltypes.py
Created August 5, 2016 15:45
Show Gist options
  • Save kevgathuku/8d2c2479c214b06a237cd50a986ae208 to your computer and use it in GitHub Desktop.
Save kevgathuku/8d2c2479c214b06a237cd50a986ae208 to your computer and use it in GitHub Desktop.
Useful sqlalchemy custom types
import json
import sqlalchemy.types
class JSONType(sqlalchemy.types.PickleType):
"""An example
>>> class User(declarative_base):
... friends = Column(JSONType())
...
... def update_friends(self, access_token):
... graph = facebook.GraphAPI(access_token)
... profile = graph.get_object("me")
... friends = graph.get_connections("me", "friends")
... if "data" in friends:
... self.friends = dict(
... [ (f["id"], f["name"]) for f in friends["data"] ]
... )
"""
impl = sqlalchemy.types.UnicodeText
def __init__(self):
sqlalchemy.types.PickleType.__init__(self, pickler=json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment