【发布时间】:2018-12-07 20:58:38
【问题描述】:
我正在尝试将 peewee 模型和类包装到其他接口中,并且我想将模型动态分配给数据库。我为此使用 peewee.Proxy 类,但我不想使用全局变量来使该代理的初始化可用。我想为更改基本模型的 Meta(内部)类创建类方法,但出现以下错误:
AttributeError: type object 'BaseModel' has no attribute 'Meta'
我拥有的代码:
import peewee as pw
class BaseModel(pw.Model):
class Meta:
database = pw.Proxy()
@classmethod
def configure_proxy(cls, database: pw.Database):
cls.Meta.database.initialize(database)
当然我可以通过调用 BaseModel.Meta.database 来访问这个变量,但在我看来它不太直观。
你有什么建议吗?
【问题讨论】:
标签: python python-3.x peewee