I am developing a API which will return entries from a Database. I am using Flask-SQLAlchemy, Flask-Marshmallow, Flask-Admin and Docker to package it all up.
In one file Packages.py I have the following code regarding the database. The class PckagesSchema is the class I've added when trying to get Flash-Marshmallow working.
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
trackingnumber = db.Column(db.String(15))
email = db.Column(db.String(80))
localid = db.Column(db.String(80))
model = Packages
from app.models import Packages, PackagesSchema
packages_schema = PackagesSchema()
result = packages_schema.dump(Packages).data
I just brushed up a little on marshmallow:
@app.route('/packages', methods=['GET']) def get_packages(): packages = Packages.query.all() result = packages_schema.dump(packages).data return jsonify(result)
Should give you everything in there. If you are returning many records, you need to add
packages_schema = PackagesSchema(many=True).
Also, not sure what version of flask you are running... but pre the latest release, I dont think jsonify would work here since you could not call it on a list. Juts a heads up.