flask-sqlalchemy与flask-migrate

flask-sqlalchemy

# 安装
pip install flask-sqlalchemy

# 所有的导入都找下面的db
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

flask-migrate

安装:
pip install flask-migrate

命令:manager.add_command('db1', MigrateCommand) # 当项目第一次执行迁移的时候,只需要初始化一次 1、python3 manage.py db1 init 2、python3 manage.py db1 migrate # 等同于django的makemigrations 3、python3 manage.py db1 upgrade # 等同于django的migrate

如何使用

目录结构

 account.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Blueprint
from .. import db
from .. import models

account = Blueprint('account', __name__)


@account.route('/login')
def login():
    # db.session.add(models.Users(username='lqz', email='123'))
    # #db.session.query(models.Users).all()
    # db.session.commit()
    # 添加示例
    """
    db.session.add(models.Users(username='lqz', pwd='123', gender=1))
    db.session.commit()

    obj = db.session.query(models.Users).filter(models.Users.id == 1).first()
    print(obj)

    PS: db.session和db.create_session
    """
    # db.session.add(models.Users(username='wupeiqi1', email='wupeiqi1@xx.com'))
    # db.session.commit()
    # db.session.close()
    #
    # db.session.add(models.Users(username='wupeiqi2', email='wupeiqi2@xx.com'))
    # db.session.commit()
    # db.session.close()
    # db.session.add(models.Users(username='alex1',email='alex1@live.com'))
    # db.session.commit()
    # db.session.close()

    user_list = db.session.query(models.Users).all()

    for item in user_list:
        print(item.username,"is",item.email)


    return 'login'
View Code

__init__.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

# from .models import *
from .views import account

def create_app():
    app = Flask(__name__)
    app.config.from_object('settings.BaseConfig')

    # 将db注册到app中
    db.init_app(app)

    # 注册蓝图
    app.register_blueprint(account.account)


    return app

models.py

from . import db

class Users(db.Model):
    """
    用户表
    """
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __repr__(self):
        return '<User %r>' % self.username

manage.py

from .sansa import create_app
from flask_script import Manager
# flask_migrate管理数据迁移的
from flask_migrate import Migrate,MigrateCommand
from .sansa import db
app = create_app()
manager=Manager(app)

# 将当前app,与db注册到Migrate
Migrate(app,db)
# 添加管理数据的命令
manager.add_command('db1', MigrateCommand)

if __name__ == '__main__':
    manager.run()

settings.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

class BaseConfig(object):
    # SESSION_TYPE = 'redis'  # session类型为redis
    # SESSION_KEY_PREFIX = 'session:'  # 保存到session中的值的前缀
    # SESSION_PERMANENT = True  # 如果设置为False,则关闭浏览器session就失效。
    # SESSION_USE_SIGNER = False  # 是否对发送到浏览器上 session:cookie值进行加密

    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:@127.0.0.1:3306/day96?charset=utf8"
    SQLALCHEMY_POOL_SIZE = 5
    SQLALCHEMY_POOL_TIMEOUT = 30
    SQLALCHEMY_POOL_RECYCLE = -1

    # 追踪对象的修改并且发送信号
    SQLALCHEMY_TRACK_MODIFICATIONS = False


class ProductionConfig(BaseConfig):
    pass


class DevelopmentConfig(BaseConfig):
    pass


class TestingConfig(BaseConfig):
    pass

 

posted @ 2020-03-23 10:10  Hank·Paul  阅读(895)  评论(0编辑  收藏  举报