#coding=utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate,MigrateCommand from flask_script import Shell,Manager
app = Flask(__name__)
manager = Manager(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1:3306/Flask_test' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) #第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例 migrate = Migrate(app,db) #manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令 manager.add_command('db',MigrateCommand) #定义模型Role class Role(db.Model): # 定义表名 __tablename__ = 'roles' # 定义列对象 id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
user = db.relationship('User', backref='role') #repr()方法显示一个可读字符串, def __repr__(self): return 'Role:'.format(self.name) #定义用户 class User(db.Model): __talbe__ = 'users' id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True) #设置外键 role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) def __repr__(self): return 'User:'.format(self.username) if __name__ == '__main__': #创建表 db.create_all() #给Role模型添加数据 admin_role = Role(name='Administer')
user_role = Role(name='User') #给User模型添加数据 user_zhangsan = User(username='zhangsan', role=admin_role)
user_lisi = User(username='lisi', role=user_role)
user_wangwu = User(username='wangwu', role=user_role)
user_zhaoliu = User(username='zhaoliu', role=user_role) #提交数据 db.session.add_all([admin_role,user_role, user_zhangsan, user_lisi, user_wangwu, user_zhaoliu])
db.session.commit()
manager.run()
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31475921/viewspace-2142129/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31475921/viewspace-2142129/