Skip to main content

Flask-SQLAlchemy Model to API in one line of code

Project description

https://travis-ci.org/acifani/flask-sqla2api.svg?branch=master https://badge.fury.io/py/Flask-SQLA2api.svg

Flask middleware that creates a simple Flask API CRUD REST endpoint based on a SQLAlchemy model definition.

Basic usage

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_sqla2api import SQLA2api

# Init app and DB
app = Flask(__name__)
db = SQLAlchemy(app)

# Setup a simple SQLAlchemy model
class Entry(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

# Init Flask-sqla2api
api = SQLA2api([Entry], self.db)
api.append_blueprints(app)

The previous tiny app will create the following endpoints

URL HTTP Method Action
/entry GET Get all entries
/entry POST Create new entry
/entry/<id> GET Get a single entry
/entry/<id> PUT Edit existing entry
/entry/<id> DELETE Delete existing entry

Generate single blueprint

If you want more control over your blueprints you can generate it and append it yourself to your app.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_sqla2api import generate_blueprint

# Init app and DB
app = Flask(__name__)
db = SQLAlchemy(app)

# Setup a simple SQLAlchemy model
class Entry(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

# Generate and register blueprint
blueprint = generate_blueprint(Entry, db)
app.register_blueprint(blueprint, url_endpoint='/')

To-Do

  • Input validation
  • API docs generation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for Flask-SQLA2api, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size Flask_SQLA2api-0.2.1-py2.7.egg (7.1 kB) File type Egg Python version 2.7 Upload date Hashes View hashes
Filename, size Flask_SQLA2api-0.2.1-py2.py3-none-any.whl (6.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size Flask-SQLA2api-0.2.1.tar.gz (4.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page