Skip to content

Building blocks for REST APIs for Flask

License

Notifications You must be signed in to change notification settings

sloria/flask-resty

 
 

Repository files navigation

Flask-RESTy Travis PyPI

Building blocks for REST APIs for Flask.

Codecov

Usage

Create a SQLAlchemy model and a marshmallow schema, then:

from flask_resty import Api, GenericModelView

from . import app, models, schemas


class WidgetViewBase(GenericModelView):
    model = models.Widget
    schema = models.WidgetSchema()


class WidgetListView(WidgetViewBase):
    def get(self):
        return self.list()

    def post(self):
        return self.create()


class WidgetView(WidgetViewBase):
    def get(self, id):
        return self.retrieve(id)

    def patch(self, id):
        return self.update(id, partial=True)

    def delete(self, id):
        return self.destroy(id)


api = Api(app, '/api')
api.add_resource('/widgets', WidgetListView, WidgetView)

By default, models are expected to have been created using Flask-SQLAlchemy.

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, String

from . import app

db = SQLAlchemy(app)


class Widget(db.Model):
    id = Column(String, primary_key=True)
    name = Column(String, nullable=False)
    color = Column(String, nullable=False)

Schemas can be standard marshmallow Schema instances or marshmallow-sqlalchemy TableSchema instances. They should not be ModelSchema instances.

from marshmallow_sqlalchemy import TableSchema

from . import models


class WidgetSchema(TableSchema):
    class Meta:
        table = models.Widget.__table__

About

Building blocks for REST APIs for Flask

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%