当前位置:网站首页>Flask-RESTful request response and SQLAlchemy foundation
Flask-RESTful request response and SQLAlchemy foundation
2022-08-02 14:20:00 【czy1206527605】
一.flask_restfun处理请求
1.处理的流程
from flask import Flask
from flask_restful import Api, Resource
from flask_restful import reqparse
app = Flask(__name__)
api = Api(app)
class Index(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('id')
args = parser.parse_args()
return '参数ID的值为: {}'.format(args['id'])
api.add_resource(Index, '/index')
if __name__ == '__main__':
app.run()
2.思路
1.导入RequestParser类
2.实例化RequestParser对象
3.向RequestParser对象中添加需要检验或转换的参数声明
4.使用parse_args()方法启动检验处理
5.通过args.id或args[‘id’]的方法获取参数
二.序列化数据
1.Serialization in the form of a decorator
from flask import Flask
from flask_restful import Api, Resource
from flask_restful import fields, marshal_with
app = Flask(__name__)
api = Api(app)
class User(object):
def __init__(self, name, age, password):
self.name = name
self.age = age
self.password = password
resource_fields = {
'name': fields.String,
'age': fields.Integer,
}
class Index(Resource):
@marshal_with(resource_fields, envelope='data')
def get(self, **kwargs):
user = User('Jeremy', 18, '123456')
return user
# 指定路由
api.add_resource(Index, '/index')
if __name__ == '__main__':
app.run()
2.marshalMethods are serialized directly
from flask import Flask
from flask_restful import Api, Resource
from flask_restful import fields, marshal_with, marshal
app = Flask(__name__)
api = Api(app)
class User(object):
def __init__(self, name, age, password):
self.name = name
self.age = age
self.password = password
resource_fields = {
'name': fields.String,
'age': fields.Integer,
}
class Index(Resource):
def get(self, **kwargs):
user = User('Jeremy', 18, '123456')
return marshal(user, resource_fields)
api.add_resource(Index, '/index')
if __name__ == '__main__':
app.run()
其中 If you want to respond backjson数据格式时 应return
{
"msg": "发送成功",
"code": 200,
"data": {
...}
}
一.SQLAlchemy
1.SQLAlchemy数据库的配置
安装Flask-SQLAlchemy
pip install flask-sqlalchemy
from flask import Flask
app = Flask(__name__)
class Config(object):
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:[email protected]:3306/jiyunstus'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = True
app.config.from_object(Config)
2.SQLAlchemyCreation of database model classes
from flask_sqlalchemy import SQLAlchemy
from app import app
db = SQLAlchemy(app)
class UserModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
3.SQLAlchemy数据库迁移文件
from flask_migrate import Migrate, MigrateCommand
from flask_script import Shell, Manager
from models.user_models import db
from app import app
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
PS:File migration command
初始化迁移文件
python manage.py db init
生成迁移文件
python manage.py db migrate
执行迁移
python manage.py db upgrade
边栏推荐
猜你喜欢
随机推荐
第十单元 前后连调
How does Apache, the world's largest open source foundation, work?
[ROS](05)ROS通信 —— 节点,Nodes & Master
idea社区版下载安装教程_安装天然气管道的流程
MobileNet ShuffleNet & yolov5替换backbone
Hands-on OCR (1)
deal!It's July 30th!
(ROS) (03) CMakeLists. TXT, rounding
MobileNet ShuffleNet & yolov5 replace backbone
vim复制粘贴_vim如何复制粘贴
Unit 5 Hold Status
window10下半自动标注
ftp常用命令详解_iftop命令详解
The world's largest Apache open source foundation is how it works?
LayoutParams的详解
Flask框架的搭建及入门
期货具体是如何开户的?
[ROS] Introduction to common tools in ROS (to be continued)
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
YOLOv7使用云GPU训练自己的数据集