-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcaf70e
commit 3fd6c51
Showing
6 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
from flask import Flask | ||
from flask.ext.sqlalchemy import SQLAlchemy | ||
from flask_sqlalchemy import SQLAlchemy | ||
from app import views, models | ||
|
||
import os | ||
from flask.ext.login import LoginManager | ||
from flask.ext.openid import OpenID | ||
from config import basedir | ||
# from flask.ext.sqlalchemy import SQLAlchemy | ||
|
||
app = Flask(__name__) | ||
app.config.from_object('config') | ||
db = SQLAlchemy(app) | ||
|
||
from app import views, models | ||
|
||
lm = LoginManager() | ||
lm.init_app(app) | ||
oid = OpenID(app, os.path.join(basedir, 'tmp')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
from app import db | ||
|
||
|
||
class User(db.Model): | ||
# 对于一个一对多的关系,db.relationship 字段通常是定义在“一”这一边 | ||
# 在这种关系下,得到一个 user.posts 成员,它给出一个用户所有的 blog | ||
id = db.Column(db.Integer, primary_key = True) | ||
nickname = db.Column(db.String(64), index = True, unique = True) | ||
email = db.Column(db.String(120), index = True, unique = True) | ||
post = db.relationship('Post', backref='author', lazy='dynamic') | ||
|
||
def __repr__(self): | ||
return '<User %r>' % self.nickname | ||
|
||
|
||
class Post(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
body = db.Column(db.String(140)) | ||
timestamp = db.Column(db.DateTime) | ||
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) | ||
|
||
def __repr__(self): | ||
return '<User %r>' % (self.nickname) | ||
return '<Post %r>' % self.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from sqlalchemy import * | ||
from migrate import * | ||
|
||
|
||
from migrate.changeset import schema | ||
pre_meta = MetaData() | ||
post_meta = MetaData() | ||
post = Table('post', post_meta, | ||
Column('id', Integer, primary_key=True, nullable=False), | ||
Column('body', String(length=140)), | ||
Column('timestamp', DateTime), | ||
Column('user_id', Integer), | ||
) | ||
|
||
|
||
def upgrade(migrate_engine): | ||
# Upgrade operations go here. Don't create your own engine; bind | ||
# migrate_engine to your metadata | ||
pre_meta.bind = migrate_engine | ||
post_meta.bind = migrate_engine | ||
post_meta.tables['post'].create() | ||
|
||
|
||
def downgrade(migrate_engine): | ||
# Operations to reverse the above upgrade go here. | ||
pre_meta.bind = migrate_engine | ||
post_meta.bind = migrate_engine | ||
post_meta.tables['post'].drop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from app import db, models | ||
''' | ||
u = models.User(nickname='Scott', email='[email protected]') | ||
db.session.add(u) | ||
db.session.commit() | ||
u = models.User(nickname='Lucine', email='[email protected]') | ||
db.session.add(u) | ||
db.session.commit() | ||
users = models.User.query.all() | ||
print(users) | ||
u = models.User.query.get(1) | ||
print(u) | ||
import datetime | ||
u = models.User.query.get(1) | ||
p = models.Post(body='my first post!', timestamp=datetime.datetime.utcnow(), author=u) | ||
db.session.add(p) | ||
db.session.commit() | ||
# get all posts from a user | ||
u = models.User.query.get(1) | ||
print(u) | ||
posts = u.posts.all() | ||
print(posts) | ||
# obtain author of each post | ||
for p in posts: | ||
print(p.id,p.author.nickname,p.body) | ||
# a user that has no posts | ||
u = models.User.query.get(2) | ||
print(u) | ||
print(u.posts.all()) | ||
# get all users in reverse alphabetical order | ||
print(models.User.query.order_by('nickname desc').all()) | ||
''' | ||
|
||
|
||
users = models.User.query.all() | ||
for u in users: | ||
db.session.delete(u) | ||
posts = models.Post.query.all() | ||
for p in posts: | ||
db.session.delete(p) | ||
db.session.commit() |