peewee - Define models seprately from Database() initialization
<p>I need to use some ORM engine, like <strong>peewee</strong>, for handling SQLite database within my python application. However, most of such libraries offer syntax like this to define <code>models.py</code>:</p>

<pre><code>import peewee

db = peewee.Database('hello.sqlite')

class Person(peewee.Model):
    name = peewee.CharField()

    class Meta:
        database = db
</code></pre>

<p>However, in my application, i cannot use such syntax since database file name is provided by outside code after import, from module, which imports my <code>models.py</code>.</p>

<p>How to initialize models from outside of their definition knowing dynamic database file name? Ideally, <code>models.py</code> should not contain "database" mentions at all, like normal ORM.</p>

<p>may be youa re lookin at proxy feature :
<a href="http://peewee.readthedocs.io/en/latest/peewee/api.html?highlight=proxy#Proxy" rel="nofollow">proxy - peewee</a></p>

<pre><code>database_proxy = Proxy()  # Create a proxy for our db.

class BaseModel(Model):
    class Meta:
        database = database_proxy  # Use proxy for our DB.

class User(BaseModel):
    username = CharField()

# Based on configuration, use a different database.
if app.config['DEBUG']:
    database = SqliteDatabase('local.db')
elif app.config['TESTING']:
    database = SqliteDatabase(':memory:')
else:
    database = PostgresqlDatabase('mega_production_db')

# Configure our proxy to use the db we specified in config.
database_proxy.initialize(database)
</code></pre>

python sqlite python-3.x orm peewee