Skip to content

Instantly share code, notes, and snippets.

@hasenj
Created April 6, 2013 15:47
Show Gist options
  • Save hasenj/5326553 to your computer and use it in GitHub Desktop.
Save hasenj/5326553 to your computer and use it in GitHub Desktop.
Connect to a ClearDB database via the peewee ORM
from peewee import *
# assume environment variable CLEARDB_DATABASE_URL is set (true on Heroku if you have the cleardb addon)
url = urlparse.urlparse(os.getenv('CLEARDB_DATABASE_URL'))
dbname = url.path[1:url.path.index('?')]
db = MySQLDatabase(dbname, host=url.hostname, user=url.username, passwd=url.password)
# Now use db to connect to the database ..
class BaseModel(Model):
"""A base model for all other models (specifies the database object)"""
class Meta:
database = db
# Define all your models to inherit from BaseModel
# for example:
class User(BaseModel):
username = CharField(index=True, unique=True)
password = CharField()
email = CharField(index=True, default='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment