Package: pyramid_peewee
Pyramid_peewee is a module for the Pyramid web application framework
which allos developers to use Peewee as their ORM.Requirements:1. Pyramid
2. Peewee
3. DatabaseUsage:Typically Pyramid based applications are configured in two locations, at
the application level within the application __init__.py file and at
the instance level within an “ini” file, such as production.ini. To
configure your applicaiton, first at the instance (eg production.ini)
level you must set peewee.urls = “database connection url”peewee.urls=sqlite:///dbase.dborpeewee.urls=postgresql://user:pass@localhost:5432/mydatabasesqlite:///dbase.dbNote we specifically use database urls as defined in the peewee
documentationConnecting using a Database
URLand that you may specify as many database urls as your application needs
within the peewee.urls paramter. At the application level, eg within
your applications __init__.py fileimport your database modelinclude the command config.include(‘pyramid_peewee’) within the
config section.Define your data model: To define your data model fist set your database
= peewee.Proxy. Note if your database contains a “.” in the name, such
as with sqlite files replace it with an “_” (underscore).frompeeweeimport*mydatabase=Proxy()dbase_db=Proxy()classPeople(Model):name=TextField()classMeta:database=mydatabaseclassCars(model):name=TextField()classMeta:database=dbase_dbIn this example we’ve defined two tables, each derived from different
database Proxies. Peewee allows you to define as many database
connections as you need and utilize them seemlessly within an
application.Using your datamodel within a View:To utilize your datamodel within a view you must first get a datbase
connection. This is accomplished by calling the database proxy which is
tied to the view request. By doing so database connections are properly
closed as the end of the request.frommodelimport*defmyView(requset):request.mydatabasejoe=People.select().where(People.name=='joe').get()returndict(name=joe.name)As stated above by calling request.mydatabase two things are
accomplished, one a database connection is opened for the request, and
two a callback is registered wich will properly close the connection at
the end of the request. Trying to use the model without first calling
the database method from the request object will result in an
DataBaseClosed error. This is a devation from the normal PeeWee behavior
which normaly opens a connection as needed.Supported database urls from the PeeWee documentation:sqlite:///my_database.dbwill create a SqliteDatabase instance for
the file my_database.db in the current directory.sqlite:///:memory:will create an in-memory SqliteDatabase
instance.postgresql://postgres:my_password@localhost:5432/my_databasewill
create a PostgresqlDatabase instance. A username and password are
provided, as well as the host and port to connect to.mysql://user:passwd@ip:port/my_dbwill create a MySQLDatabase
instance for the local MySQL databasemy_db.Supported schemes:apsw: APSWDatabasemysql: MySQLDatabasemysql+pool: PooledMySQLDatabasepostgres: PostgresqlDatabasepostgres+pool: PooledPostgresqlDatabasepostgresext: PostgresqlExtDatabasepostgresext+pool: PooledPostgresqlExtDatabasesqlite: SqliteDatabasesqliteext: SqliteExtDatabaseThe most up to date documentation can always be found on their website:Peewee Database
URL