答案 0 :(得分:1)
您可以使用已记录的Database.bind()
或Database.bind_ctx()
方法:
http://docs.peewee-orm.com/en/latest/peewee/api.html#Database.bind_ctx
文档涵盖了这种确切的情况:
MODELS = (User, Account, Note)
# Bind the given models to the db for the duration of wrapped block.
def use_test_database(fn):
@wraps(fn)
def inner(self):
with test_db.bind_ctx(MODELS):
test_db.create_tables(MODELS)
try:
fn(self)
finally:
test_db.drop_tables(MODELS)
return inner
class TestSomething(TestCase):
@use_test_database
def test_something(self):
# ... models are bound to test database ...
pass