当前位置:网站首页>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
边栏推荐
猜你喜欢
[ROS](05)ROS通信 —— 节点,Nodes & Master
What are the file encryption software?Keep your files safe
yolov5,yolov4,yolov3 mess
Deep learning framework pytorch rapid development and actual combat chapter4
window10下半自动标注
Building and getting started with the Flask framework
How does Apache, the world's largest open source foundation, work?
Object detection scene SSD-Mobilenetv1-FPN
浅浅写一下PPOCRLabel的使用及体验
[ROS] (06) ROS Communication - Topic Communication
随机推荐
Tornado框架路由系统介绍及(IOloop.current().start())启动源码分析
Unit 12 associated serialization
Chapter6 visualization (don't want to see the version)
LayoutParams的详解
Go语言初始
【VCU】详解S19文件(S-record)
php开源的客服系统_在线客服源码php
Flask框架深入二
8580 Merge linked list
8581 线性链表逆置
web测试和app测试的区别?
Unit 11 Serializers
MobileNet ShuffleNet & yolov5替换backbone
关于市场后市的发展预测? 2021-05-23
第十五单元 分页、过滤
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
[ROS] (04) Detailed explanation of package.xml
Object detection scene SSD-Mobilenetv1-FPN
动手学ocr(一)
Unit 6 meet ORM