Improved documentation for model registration
This commit is contained in:
parent
9ae4eba986
commit
24c1713ff5
1 changed files with 9 additions and 0 deletions
|
|
@ -47,6 +47,10 @@ Here the example `database.py` module for your application::
|
|||
Base.query = db_session.query_property()
|
||||
|
||||
def init_db():
|
||||
# import all modules here that might define models so that
|
||||
# they will be registered properly on the metadata. Otherwise
|
||||
# you will have to import them first before calling init_db()
|
||||
import yourapplication.models
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
To define your models, just subclass the `Base` class that was created by
|
||||
|
|
@ -84,6 +88,11 @@ Here is an example model (put this into `models.py`, e.g.)::
|
|||
def __repr__(self):
|
||||
return '<User %r>' % (self.name)
|
||||
|
||||
To create the database you can use the `init_db` function:
|
||||
|
||||
>>> from yourapplication.database import init_db
|
||||
>>> init_db()
|
||||
|
||||
You can insert entries into the database like this:
|
||||
|
||||
>>> from yourapplication.database import db_session
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue