带"Using"装饰器的皮尤多数据库



在peewee文档中,它说您可以使用"decorator来驾驭多个数据库

master = PostgresqlDatabase('master')
read_replica = PostgresqlDatabase('replica')
class Data(Model):
value = IntegerField()
class Meta:
database = master
with Using(read_replica, [Data]):
# Query is executed against the read replica.
Data.get(Data.value == 5)
# Since we did not specify this model in the list of overrides
# it will use whatever database it was defined with.
SomeOtherModel.get(SomeOtherModel.field == 3)

在上面的例子中,你可以使用"Using"使用多个数据库。decorator。

我的问题是如何导入"Using"装饰吗?

我找不到任何关于导入Using装饰器的代码。

你在2号。X文档,尝试使用文档的版本来匹配您正在使用的peewee版本。自3.0以来,使用bind()和bind_ctx()方法来完成此操作:

http://docs.peewee-orm.com/en/latest/peewee/api.html Database.bind_ctx

master = PostgresqlDatabase('master')
read_replica = PostgresqlDatabase('replica')
class Data(Model):
value = IntegerField()
class Meta:
database = master
with read_replica.bind_ctx([Data]):
# Query is executed against the read replica.
Data.get(Data.value == 5)
# Since we did not specify this model in the list of overrides
# it will use whatever database it was defined with.
SomeOtherModel.get(SomeOtherModel.field == 3)

相关内容

  • 没有找到相关文章

最新更新