Created
April 6, 2013 15:47
-
-
Save hasenj/5326553 to your computer and use it in GitHub Desktop.
Connect to a ClearDB database via the peewee ORM
This file contains 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
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