Peewee – SQLite扩展
Peewee有一个Playhouse命名空间。它是各种扩展模块的集合。其中一个是 playhouse.sqlite_ext 模块。It mainly defines SqliteExtDatabase class which inherits SqliteDatabase class, supports following additional features −
SQLite扩展的特点
Peewee支持的SQLite扩展的特点如下
- 全文搜索。
 - 
JavaScript Object Notation (JSON)扩展集成。
 - 
支持闭合表扩展。
 - 
LSM1扩展支持。
 - 
用户定义的表函数。
 - 
支持使用备份API的在线备份:backup_to_file()。
 - 
支持BLOB API,用于高效的二进制数据存储。
 
可以存储JSON数据,如果一个特殊的 JSONField 被声明为字段属性之一。
class MyModel(Model):
   json_data = JSONField(json_dumps=my_json_dumps)
为了激活全文搜索,模型可以有 DocIdField 来定义主键。
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会将它们视为文本)。
SearchField是一个字段类,用于代表全文搜索虚拟表的模型上的列。
SqliteDatabase支持AutoField来增加主键。然而,SqliteExtDatabase支持AutoIncrementField以确保主键总是单调地增加,而不考虑行的删除。
playhouse命名空间的SqliteQ模块(playhouse.sqliteq)定义了SqliteExeDatabase的子类,以处理对SQlite数据库的串行并发写入。
另一方面,playhouse.apsw 模块支持 apsw sqlite 驱动。另一个Python SQLite Wrapper (APSW)是快速的,并且可以处理嵌套事务,这些事务由你的代码明确管理。
from apsw_ext import *
db = APSWDatabase('testdb')
class BaseModel(Model):
   class Meta:
      database = db
class MyModel(BaseModel):
   field1 = CharField()
   field2 = DateTimeField()
极客教程