-
Notifications
You must be signed in to change notification settings - Fork 17
/
db_action.py
40 lines (31 loc) · 992 Bytes
/
db_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
sys.path.append('../')
from app.models import User, Post
from app import app, db
def run():
db.create_all()
user = User(username='ddxygq', email='ddxygq@gmail.com')
user2 = User(username='cj318.cn', email='hbzegkg@126.com')
user.set_password('123456')
db.session.add(user)
user2.set_password('123456')
db.session.add(user2)
db.session.commit()
u = User.query.get(1)
# post有一个动态属性,post.author,将返回发出动态的user
for i in range(5):
body = '%s post %s' % (u.username, str(i))
print(body)
p = Post(body=body, author=u)
db.session.add(p)
db.session.commit()
posts = Post.query.all()
for post in posts:
print(post.id, post.body, post.timestamp, post.user_id, post.author)
def show_user():
users = User.query.all()
for u in users:
print(u.id, u.username, u.email, u.password_hash)
if __name__ == '__main__':
# run()
show_user()