Skip to content
Myr

Improved documentation for model registration

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 9ae4eba · Browse files · 1 file+9 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+9 added−0 removed

  • other:+9 added−0 removed

1 file+9 added−0 removed

modified:docs/patterns/sqlalchemy.rst+9 added
45 unmodified lines
46 Base = declarative_base()47 Base.query = db_session.query_property()4849 def init_db():50 # import all modules here that might define models so that51 # they will be registered properly on the metadata. Otherwise52 # you will have to import them first before calling init_db()53 import yourapplication.models54 Base.metadata.create_all(bind=engine)5556To define your models, just subclass the `Base` class that was created by57the code above. If you are wondering why we don't have to care about
29 unmodified lines
8788 def __repr__(self):89 return '<User %r>' % (self.name)9091To create the database you can use the `init_db` function:9293>>> from yourapplication.database import init_db94>>> init_db()9596You can insert entries into the database like this:9798>>> from yourapplication.database import db_session99>>> from yourapplication.models import User
117 unmodified lines