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.

  1. database = PostgresqlDatabase(None) # Un-initialized database.
  2.  
  3. class SomeModel(Model):
  4. class Meta:
  5. database = database

If you try to connect or issue any queries while your database is uninitializedyou will get an exception:

  1. >>> database.connect()
  2. 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:

  1. database_name = input('What is the name of the db? ')
  2. database.init(database_name, host='localhost', user='postgres')

For even more control over initializing your database, see the next section,Dynamically defining a database.