def insert_data(): db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_david = User(username='******', role=user_role) db.session.add_all( [admin_role, mod_role, user_role, user_john, user_susan, user_david]) db.session.commit()
# !/usr/bin/env python # -*- coding: utf-8 -*- # Author : 湛允鹏 # Link : from hello import db from hello import User, Role db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_roel = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_steve = User(username='******', role=user_role) db.session.add_all( [admin_role, mod_roel, user_role, user_john, user_susan, user_steve]) db.session.commit() print(admin_role.id) print(mod_roel.id) print(user_role.id) admin_role.name = 'Administrator' db.session.add(admin_role) db.session.commit() db.session.delete(mod_roel) db.session.commit()
# coding:utf-8 from hello import db from hello import Role, User db.create_all() db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_dada = User(username='******', role=mod_role) print(admin_role.id) print(mod_role.id) print(user_role.id) db.session.add(admin_role) db.session.add(mod_role) db.session.add(user_role) db.session.add(user_john) db.session.add(user_dada) db.session.add(user_susan) db.session.commit() # 数据库会话 db.session 与 Flask session 对象没有关系。数据库会话也称为事务。 print(admin_role.id)
from hello import db, Role, User db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_tony = User(username='******', role=user_role) db.session.add(Role(name='foobar')) db.session.add(admin_role) db.session.add(mod_role) db.session.add(user_role) db.session.add(user_john) db.session.add(user_susan) db.session.add(user_tony) db.session.commit()
from hello import app from hello import db from hello import User, Role print app.url_map user_role = Role(name='user') user_gaoshine = User(username='******', role=user_role)