Molitoris
Molitoris

Reputation: 1035

How to initialize a peewee SQLite database at runtime?

I'm ussing peewee for my sqlite database. I wrote a module which contains the database models. In the first code snippet of the doc it is explained how the database is initialized:

db_model.py

import datetime
from peewee import *

db = SqliteDatabase('my_app.db')

class BaseModel(Model):
    class Meta:
        database = db

class User(BaseModel):
    username = CharField(unique=True)

class Tweet(BaseModel):
    user = ForeignKeyField(User, backref='tweets')
    message = TextField()
    created_date = DateTimeField(default=datetime.datetime.now)
    is_published = BooleanField(default=True)

In my understanding, the database is initialized the moment the module is import? I am looking for a way to initialize the database during runtime? How can I achieve this?

Upvotes: 2

Views: 1142

Answers (1)

coleifer
coleifer

Reputation: 26245

Read the docs. "Run-time database configuration" seems relevant:

http://docs.peewee-orm.com/en/latest/peewee/database.html#run-time-database-configuration

For instance, initialize the db object with None:

db = SqliteDatabase(None)

... and later in your application:

db.init(<actual parameters>)

Upvotes: 2

Related Questions