利用peewee作为您的orm与金字塔网络应用程序。-金字塔
pyramid_peewee的Python项目详细描述
金字塔是金字塔Web应用程序框架的一个模块 允许开发者使用peewee作为orm。
要求:
1. Pyramid 2. Peewee 3. Database
用法:
通常基于金字塔的应用程序配置在两个位置 应用程序init.py文件中的应用程序级别 “ini”文件中的实例级别,如production.ini。到 首先在实例中配置应用程序(例如production.ini) 级别必须设置peewee.url=“database connection url”
peewee.urls=sqlite:///dbase.dborpeewee.urls= postgresql://user:pass@localhost:5432/mydatabase sqlite:///dbase.db
注意,我们特别使用peewee中定义的数据库url 文档Connecting using a Database URL 您可以根据应用程序的需要指定任意多个数据库url 在peewee.urls参数中。在应用程序级别,如 您的应用程序初始化文件
- 导入数据库模型
- 在 配置部分。
定义数据模型:若要定义数据模型,请首先设置数据库 =peewee.proxy.代理。注意,如果数据库名称中包含“.”,则 与sqlite文件一样,将其替换为“u”(下划线)。
frompeeweeimport*mydatabase=Proxy()dbase_db=Proxy()classPeople(Model):name=TextField()classMeta:database=mydatabaseclassCars(model):name=TextField()classMeta:database=dbase_db
在本例中,我们定义了两个表,每个表都来自不同的 数据库代理。peewee允许您定义尽可能多的数据库 根据需要建立联系,并在 申请。
在视图中使用数据模型:
要在视图中使用数据模型,必须首先获取一个数据库 连接。这是通过调用 与查看请求绑定。通过这样做,数据库连接是正确的 请求结束时关闭。
frommodelimport*defmyView(requset):request.mydatabasejoe=People.select().where(People.name=='joe').get()returndict(name=joe.name)
如上所述,通过调用request.mydatabase有两件事 完成后,为请求打开一个A数据库连接,并且 已注册回调,将在正确关闭连接 请求的结束。尝试在不首先调用的情况下使用模型 来自请求对象的数据库方法将导致 数据库关闭错误。这是对正常小便行为的贬低 根据需要打开一个连接。
peewee文档中支持的数据库url:
- sqlite:///my_database.db将为 当前目录中的文件my_database.db。
- sqlite://:内存:将创建内存中的sqlite数据库 实例。
- postgresql://postgres:my_password@localhost:5432/my_database将 创建PostgreSqlDatabase实例。用户名和密码是 以及要连接到的主机和端口。
- mysql://user:passwd@ip:port/my db将创建mysqldatabase 本地mysql数据库的实例my_db。
支持的方案:
- apsw:apswdatabase
- mysql:mysqldatabase
- mysql+池:pooledmysqldatabase
- postgres:postgresqldatabase
- Postgres+池:pooledPostgreSqlDatabase
- postgresxt:postgresqlextdatabase
- PostgresExt+池:pooledPostgreSqlExtDatabase
- sqlite:sqlite数据库
- sqliteext:sqliteextdatabase
最新的文档始终可以在他们的网站上找到: Peewee Database URL