Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Flask + marshmallow for beautiful APIs

License

NotificationsYou must be signed in to change notification settings

marshmallow-code/flask-marshmallow

Repository files navigation

Latest versionBuild statusDocumentationmarshmallow 3|4 compatible

Flask + marshmallow for beautiful APIs

Flask-Marshmallow is a thin integration layer forFlask (a Python web framework) andmarshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates withFlask-SQLAlchemy.

Get it now

pip install flask-marshmallow

Create your app.

fromflaskimportFlaskfromflask_marshmallowimportMarshmallowapp=Flask(__name__)ma=Marshmallow(app)

Write your models.

fromyour_ormimportModel,Column,Integer,String,DateTimeclassUser(Model):email=Column(String)password=Column(String)date_created=Column(DateTime,auto_now_add=True)

Define your output format with marshmallow.

classUserSchema(ma.Schema):email=ma.Email()date_created=ma.DateTime()# Smart hyperlinking_links=ma.Hyperlinks(        {"self":ma.URLFor("user_detail",values=dict(id="<id>")),"collection":ma.URLFor("users"),        }    )user_schema=UserSchema()users_schema=UserSchema(many=True)

Output the data in your views.

@app.route("/api/users/")defusers():all_users=User.all()returnusers_schema.dump(all_users)@app.route("/api/users/<id>")defuser_detail(id):user=User.get(id)returnuser_schema.dump(user)# {#     "email": "fred@queen.com",#     "date_created": "Fri, 25 Apr 2014 06:02:56 -0000",#     "_links": {#         "self": "/api/users/42",#         "collection": "/api/users/"#     }# }

Learn More

To learn more about marshmallow, check out itsdocs.

Project Links

License

MIT licensed. See the bundledLICENSE file for more details.

Packages

No packages published

Contributors21

Languages


[8]ページ先頭

©2009-2025 Movatter.jp