Peewee 简明教程
Peewee - SQLite Extensions
Peewee 带有一个 Playhouse 名称空间。它是一个由各种扩展模块组成的集合。其中之一是 playhouse.sqlite_ext 模块。它主要定义 SqliteExtDatabase 类,该类继承 SqliteDatabase 类,支持以下附加功能:
Peewee comes with a Playhouse namespace. It is a collection of various extension modules. One of them is a playhouse.sqlite_ext module. It mainly defines SqliteExtDatabase class which inherits SqliteDatabase class, supports following additional features −
Features of SQLite Extensions
Peewee 支持的 SQLite 扩展功能如下 −
The features of SQLite Extensions which are supported by Peewee are as follows −
-
Full-text search.
-
JavaScript Object Notation (JSON) extension integration.
-
Closure table extension support.
-
LSM1 extension support.
-
User-defined table functions.
-
Support for online backups using backup API: backup_to_file().
-
BLOB API support, for efficient binary data storage.
如果特别 JSONField 声明为某个字段属性,则可以存储 JSON 数据。
JSON data can be stored, if a special JSONField is declared as one of the field attributes.
class MyModel(Model):
json_data = JSONField(json_dumps=my_json_dumps)
要激活全文搜索,模型可以使用 DocIdField 来定义主键。
To activate full-text search, the model can have DocIdField to define primary key.
class NoteIndex(FTSModel):
docid = DocIDField()
content = SearchField()
class Meta:
database = db
FTSModel 是 VirtualModel 的子类,可在 http://docs.peewee-orm.com/en/latest/peewee/sqlite_ext.html#VirtualModel 获得,与 FTS3 和 FTS4 全文搜索扩展一起使用。Sqlite 将所有列类型都视为 TEXT(尽管你可以存储其他数据类型,Sqlite 会将它们视为文本)。
FTSModel is a Subclass of VirtualModel which is available at http://docs.peewee-orm.com/en/latest/peewee/sqlite_ext.html#VirtualModel to be used with the FTS3 and FTS4 full-text search extensions. Sqlite will treat all column types as TEXT (although, you can store other data types, Sqlite will treat them as text).
SearchField 是一个 Field 类,用于模型中表示全文搜索虚拟表的列。
SearchField is a Field-class to be used for columns on models representing full-text search virtual tables.
SqliteDatabase 支持 AutoField 以增加主键。但是,SqliteExtDatabase 支持 AutoIncrementField 以确保主键始终呈单调递增,而不论是否删除行。
SqliteDatabase supports AutoField for increasing primary key. However, SqliteExtDatabase supports AutoIncrementField to ensure that primary always increases monotonically, irrespective of row deletions.
playhouse 命名空间 (playhouse.sqliteq) 中的 SqliteQ 模块定义了 SqliteExeDatabase 的一个子类,以处理对 SQLite 数据库的序列化并发写操作。
SqliteQ module in playhouse namespace (playhouse.sqliteq) defines subclass of SqliteExeDatabase to handle serialised concurrent writes to a SQlite database.
另一方面,playhouse.apsw 模块提供了对 apsw sqlite 驱动的支持。另一个 Python SQLite 封装器 (APSW) 速度快且可以处理嵌套事务,而这些事务是由你的代码显式管理的。
On the other hand, playhouse.apsw module carries support for apsw sqlite driver. Another Python SQLite Wrapper (APSW) is fast and can handle nested transactions, that are managed explicitly by you code.
from apsw_ext import *
db = APSWDatabase('testdb')
class BaseModel(Model):
class Meta:
database = db
class MyModel(BaseModel):
field1 = CharField()
field2 = DateTimeField()