Skip to content

Commit

Permalink
Adding notes to the cookbook on deferring db initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed May 9, 2012
1 parent dbe34ea commit aebefe1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/peewee/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ instantiate your database with ``threadlocals=True``:
concurrent_db = SqliteDatabase('stats.db', threadlocals=True)
Deferring initialization
^^^^^^^^^^^^^^^^^^^^^^^^

Sometimes the database information is not known until run-time, when it might
be loaded from a configuration file/etc. In this case, you can "defer" the initialization
of the database by passing in ``None`` as the database_name.

.. code-block:: python
deferred_db = peewee.SqliteDatabase(None)
class SomeModel(peewee.Model):
class Meta:
database = deferred_db
If you try to connect or issue any queries while your database is uninitialized
you will get an exception:

.. code-block:: python
>>> deferred_db.connect()
Exception: Error, database not properly initialized before opening connection
To initialize your database, you simply call the ``init`` method with the database_name
and any additional kwargs:

.. code-block:: python
database_name = raw_input('What is the name of the db? ')
deferred_db.init(database_name)
Creating, Reading, Updating and Deleting
----------------------------------------

Expand Down

0 comments on commit aebefe1

Please sign in to comment.