首先安裝flask_restful三方組件sql
pip install flask_restful
在models.py中新建一個類,生成表,往裏面插入一些數據。(flask要想使用ORM的話須要安裝flask_sqlalchemy三方組件,以前已經說過了,此處再也不贅述)flask
實例化咱們的api,把api對象和app綁定,而後api
restful
from flask_restful import Api from myapp.api import * api = Api() def init_api(app): api.init_app(app) api.add_resource(One,'/one') api.add_resource(Two,'/two') api.add_resource(Three,'/three/<int:id>') api.add_resource(Four,'/four/<int:page>/<int:per_page>') api.add_resource(Five,'/five') api.add_resource(Six,'/six') api.add_resource(Seven,'/seven')
__init__.pycookie
from flask import Flask from myapp.api_urls import init_api from myapp.ext import init_ext from myapp.settings import conf def create_app(env_name): app = Flask(__name__) app.config.from_object(conf.get(env_name,'debug')) init_ext(app) init_api(app) return app
輸出字段與參數解析的不一樣實現session
from flask import request from flask_restful import Resource, marshal_with, fields, reqparse from myapp.models import *
#輸出字段
#字典套字符串 one_fields = { 'id':fields.Integer(default=1), #default設置爲默認值 'user':fields.String(attribute='name'), #attribute設置爲映射到models中的name字段 'content':fields.String, 'hahaha':fields.String(default='lalala') } class One(Resource): @marshal_with(one_fields) def get(self,*args,**kwargs): id = int(request.args.get('id')) data = News.query.get(id) return data
#字典套列表 two_fields = { 'id':fields.Integer(default=1), 'name':fields.String(default='wusir'), 'hobby':fields.List(fields.String) } class Two(Resource): @marshal_with(two_fields) def get(self): hobby = ['閱讀','運動','敲代碼'] return {'hobby':hobby} #字典套字典 three_fields = { 'id': fields.Integer(default=1), 'name': fields.String(default='alex'), 'content':fields.Nested(one_fields) } class Three(Resource): @marshal_with(three_fields) def get(self,id): news = News.query.get(id) return {'content':news}
#字典套列表,列表再套字典 four_fields = { 'id':fields.Integer(default=1), 'name':fields.String(default='wusir'), 'content':fields.List(fields.Nested(one_fields)) } class Four(Resource): @marshal_with(four_fields) def get(self,page,per_page): news = News.query.paginate(page,per_page,error_out=False) #分頁實現 return {'content':news.items}
#參數解析 five_args = reqparse.RequestParser() five_args.add_argument('id',type=int,required=True,help='id是必填字段,趕忙的填上') #required爲True表示是必填字段,help爲錯誤提示信息 five_args.add_argument('name',dest='my_name') #dest表示起別名 five_args.add_argument('hobby',action='append') #action='append'表示字段能夠追加寫多個 class Five(Resource): def get(self): my_args = five_args.parse_args() print(my_args) print(my_args.get('hobby')) return {'msg':'ok'}
six_args = reqparse.RequestParser() six_args.add_argument('content',location='form') class Six(Resource): def get(self): my_args = six_args.parse_args() print(my_args) return {'msg':'get'} def post(self): my_args = six_args.parse_args() print(my_args) return {'msg':'post'}
seven_args = five_args.copy() seven_args.replace_argument('id',type=int,help='隨便你填不填',required=True) seven_args.remove_argument('name') seven_args.remove_argument('hobby') class Seven(Resource): def get(self): my_args = seven_args.parse_args() print(my_args) return {'msg':'好了'}
參數解析的位置:app
# 從post請求的form裏拿參數 parser.add_argument('name', type=int, location='form') # 從get請求的args裏拿參數 parser.add_argument('PageSize', type=int, location='args') # 從請求頭拿參數 headers parser.add_argument('User-Agent', location='headers') # 從cookies拿參數 parser.add_argument('session_id', location='cookies') # 獲取文件 parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
location 指定爲一個列表函數
文檔參考:https://flask-restful.readthedocs.io/en/latest/post