@@ -18,7 +18,7 @@ then create the :class:`SQLAlchemy` object by passing it the application.
18
18
19
19
Once created, that object then contains all the functions and helpers
20
20
from both :mod: `sqlalchemy ` and :mod: `sqlalchemy.orm `. Furthermore it
21
- provides a class called `Model ` that is a declarative base which can be
21
+ provides a class called `` Model ` ` that is a declarative base which can be
22
22
used to declare models::
23
23
24
24
from flask import Flask
@@ -44,29 +44,29 @@ used to declare models::
44
44
To create the initial database, just import the `db ` object from an
45
45
interactive Python shell and run the
46
46
:meth: `SQLAlchemy.create_all ` method to create the
47
- tables and database:
47
+ tables and database::
48
48
49
- >>> from yourapplication import db
50
- >>> db.create_all()
49
+ >>> from yourapplication import db
50
+ >>> db.create_all()
51
51
52
- Boom, and there is your database. Now to create some users:
52
+ Boom, and there is your database. Now to create some users::
53
53
54
- >>> from yourapplication import User
55
- >>>
admin = User(' admin' , ' [email protected] ' )
56
- >>>
guest = User(' guest' , ' [email protected] ' )
54
+ >>> from yourapplication import User
55
+ >>> admin = User('admin', '[email protected] ')
56
+ >>> guest = User('guest', '[email protected] ')
57
57
58
- But they are not yet in the database, so let's make sure they are:
58
+ But they are not yet in the database, so let's make sure they are::
59
59
60
- >>> db.session.add(admin)
61
- >>> db.session.add(guest)
62
- >>> db.session.commit()
60
+ >>> db.session.add(admin)
61
+ >>> db.session.add(guest)
62
+ >>> db.session.commit()
63
63
64
- Accessing the data in database is easy as a pie:
64
+ Accessing the data in database is easy as a pie::
65
65
66
- >>> users = User.query.all()
67
- [<User u'admin'>, <User u'guest'>]
68
- >>> admin = User.query.filter_by(username = ' admin' ).first()
69
- <User u'admin'>
66
+ >>> User.query.all()
67
+ [<User u'admin'>, <User u'guest'>]
68
+ >>> User.query.filter_by(username='admin').first()
69
+ <User u'admin'>
70
70
71
71
Simple Relationships
72
72
--------------------
@@ -75,7 +75,6 @@ SQLAlchemy connects to relational databases and what relational databases
75
75
are really good at are relations. As such, we shall have an example of an
76
76
application that uses two tables that have a relationship to each other::
77
77
78
-
79
78
from datetime import datetime
80
79
81
80
@@ -111,24 +110,24 @@ application that uses two tables that have a relationship to each other::
111
110
def __repr__(self):
112
111
return '<Category %r>' % self.name
113
112
114
- First let's create some objects:
113
+ First let's create some objects::
115
114
116
- >>> py = Category(' Python' )
117
- >>> p = Post(' Hello Python!' , ' Python is pretty cool' , py)
118
- >>> db.session.add(py)
119
- >>> db.session.add(p)
115
+ >>> py = Category('Python')
116
+ >>> p = Post('Hello Python!', 'Python is pretty cool', py)
117
+ >>> db.session.add(py)
118
+ >>> db.session.add(p)
120
119
121
- Now because we declared `posts ` as dynamic relationship in the backref
122
- it shows up as query:
120
+ Now because we declared `` posts ` ` as dynamic relationship in the backref
121
+ it shows up as query::
123
122
124
- >>> py.posts
125
- <sqlalchemy.orm.dynamic.AppenderBaseQuery object at 0x1027d37d0>
123
+ >>> py.posts
124
+ <sqlalchemy.orm.dynamic.AppenderBaseQuery object at 0x1027d37d0>
126
125
127
126
It behaves like a regular query object so we can ask it for all posts that
128
- are associated with our test “ Python” category:
127
+ are associated with our " Python" category: :
129
128
130
- >>> py.posts.all()
131
- [<Post 'Hello Python!'>]
129
+ >>> py.posts.all()
130
+ [<Post 'Hello Python!'>]
132
131
133
132
134
133
Road to Enlightenment
@@ -140,15 +139,15 @@ The only things you need to know compared to plain SQLAlchemy are:
140
139
141
140
- all the functions and classes from :mod: `sqlalchemy ` and
142
141
:mod: `sqlalchemy.orm `
143
- - a preconfigured scoped session called `session `
142
+ - a preconfigured scoped session called `` session ` `
144
143
- the :attr: `~SQLAlchemy.metadata `
145
144
- the :attr: `~SQLAlchemy.engine `
146
145
- a :meth: `SQLAlchemy.create_all ` and :meth: `SQLAlchemy.drop_all `
147
146
methods to create and drop tables according to the models.
148
147
- a :class: `Model ` baseclass that is a configured declarative base.
149
148
150
149
2. The :class: `Model ` declarative base class behaves like a regular
151
- Python class but has a `query ` attribute attached that can be used to
150
+ Python class but has a `` query ` ` attribute attached that can be used to
152
151
query the model. (:class: `Model ` and :class: `BaseQuery `)
153
152
154
153
3. You have to commit the session, but you don't have to remove it at
0 commit comments