原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12145936.html
PIP Dependency
pip install flask pip install flask-sqlalchemy pip install pymysql pip install flask-marshmallow pip install marshmallow-sqlalchemy
Python SRC
mysql_flask.py
1 from flask import Flask, request, jsonify, make_response 2 from flask_sqlalchemy import SQLAlchemy 3 from marshmallow_sqlalchemy import ModelSchema 4 from marshmallow import fields 5 6 app = Flask(__name__) 7 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/test' 8 db = SQLAlchemy(app) 9 10 11 class Authors(db.Model): 12 id = db.Column(db.Integer, primary_key=True) 13 name = db.Column(db.String(20)) 14 specialisation = db.Column(db.String(50)) 15 16 def create(self): 17 db.session.add(self) 18 db.session.commit() 19 return self 20 21 def __init__(self, name, specialisation): 22 self.name = name 23 self.specialisation = specialisation 24 25 def __repr__(self): 26 return '<Author %d>' % self.id 27 28 29 db.create_all() 30 31 32 class AuthorsSchema(ModelSchema): 33 class Meta(ModelSchema.Meta): 34 model = Authors 35 sqla_session = db.session 36 37 id = fields.Number(dump_only=True) 38 name = fields.String(required=True) 39 specialisation = fields.String(required=True) 40 41 42 @app.route('/authors', methods=['GET']) 43 def index(): 44 get_authors = Authors.query.all() 45 author_schema = AuthorsSchema(many=True) 46 authors = author_schema.dump(get_authors) 47 return make_response(jsonify({"authors": authors})) 48 49 50 @app.route('/authors/<id>', methods=['GET']) 51 def get_author_by_id(id): 52 get_author = Authors.query.get(id) 53 author_schema = AuthorsSchema() 54 author = author_schema.dump(get_author) 55 return make_response(jsonify({"author": author})) 56 57 58 @app.route('/authors', methods=['POST']) 59 def create_author(): 60 data = request.get_json() 61 author_schema = AuthorsSchema() 62 author = author_schema.load(data) 63 result = author_schema.dump(author.create()) 64 return make_response(jsonify({"author": result}), 201) 65 66 67 @app.route('/authors/<id>', methods=['PUT']) 68 def update_author_by_id(id): 69 data = request.get_json() 70 get_author = Authors.query.get(id) 71 if data.get('specialisation'): 72 get_author.specialisation = data['specialisation'] 73 if data.get('name'): 74 get_author.name = data['name'] 75 db.session.add(get_author) 76 db.session.commit() 77 author_schema = AuthorsSchema(only=['id', 'name', 'specialisation']) 78 author = author_schema.dump(get_author) 79 return make_response(jsonify({"author": author})) 80 81 82 @app.route('/authors/<id>', methods=['DELETE']) 83 def delete_author_by_id(id): 84 get_author = Authors.query.get(id) 85 db.session.delete(get_author) 86 db.session.commit() 87 return make_response("", 204) 88 89 90 if __name__ == "__main__": 91 app.run(debug=True)
Run
POST
$ curl http://localhost:5000/authors -d '{"name" : "Hello","specialisation" : "Python"}' -X POST -H 'Content-Type: application/json' { "author": { "id": 11.0, "name": "Hello", "specialisation": "Python" } }
GET - All
$ curl http://localhost:5000/authors { "authors": [ { "id": 11.0, "name": "Hello", "specialisation": "Python" } ] }
GET - One
1 $ curl http://localhost:5000/authors/11 2 { 3 "author": { 4 "id": 11.0, 5 "name": "Hello", 6 "specialisation": "Python" 7 } 8 }
PUT
$ curl http://localhost:5000/authors/11 -d '{"specialisation" : "Flask"}' -X PUT -H 'Content-Type: application/json' { "author": { "id": 11.0, "name": "Hello", "specialisation": "Flask" } }
DELETE
curl http://localhost:5000/authors/11 -X DELETE
Reference
https://pypi.org/project/Flask/
https://pypi.org/project/Flask-SQLAlchemy/
https://pypi.org/project/PyMySQL/
https://pypi.org/project/flask-marshmallow/
https://pypi.org/project/marshmallow-sqlalchemy/