The problem is as follows. I can not set up a test environment in a Flask application with peewee orm.
I tried to install models Proxy db

import peewee DATABASE_DATA = { 'database': 'plan', 'user': 'plan', 'password': 'plan' } TEST_DATABASE_DATA = { 'database': 'plan_test', 'user': 'plan', 'password': 'plan' } proxy_db = peewee.Proxy() class PlanModel(peewee.Model): class Meta: database = peewee.Proxy() def set_database(testing_mode=False): if testing_mode: db_data = TEST_DATABASE_DATA else: db_data = DATABASE_DATA db = peewee.PostgresqlDatabase(db_data) proxy_db.initialize(db) return db 


, but I get in the end
AttributeError: Cannot use uninitialized Proxy
Maybe you have some cookbook on this topic. I also do not understand how they run in flask, the code of the type below is not at all clear to me

 unittest.main() 

    1 answer 1

    If you want to use different databases, it is done this way: The base class for models is declared:

     from peewee import * """Used for switching between different databases(sqlite,mysql)""" database_proxy = Proxy() class BaseModel(Model): """Base class of peewee model, used for define model database""" class Meta: database = database_proxy 

    Add a successor:

     class City(BaseModel): id = PrimaryKeyField(db_column='city_id') name = CharField(db_column='city_name', max_length=255) 

    Initiate a connection by creating one of the classes:

     class Sqlite: def __init__(self): print("Open sqlite connection") self._db = SqliteDatabase(":memory:") database_proxy.initialize(self._db) class MySql: def __init__(self): print("Open MySql connection") self._db = MySQLDatabase( config.mysql["db"], host=config.mysql["host"], port=config.mysql["port"], user=config.mysql["user"], passwd=config.mysql["password"]) database_proxy.initialize(self._db) 

    Peewee dynamic db define