Run-time database configuration
Sometimes the database connection settings are not known until run-time, whenthese values may be loaded from a configuration file or the environment. Inthese cases, you can defer the initialization of the database by specifyingNone
as the database_name.
- database = PostgresqlDatabase(None) # Un-initialized database.
- class SomeModel(Model):
- class Meta:
- database = database
If you try to connect or issue any queries while your database is uninitializedyou will get an exception:
- >>> database.connect()
- Exception: Error, database not properly initialized before opening connection
To initialize your database, call the init()
method with thedatabase name and any additional keyword arguments:
- database_name = input('What is the name of the db? ')
- database.init(database_name, host='localhost', user='postgres')
For even more control over initializing your database, see the next section,Dynamically defining a database.