QuickstartΒΆ
For the restless:
1import flask
2import flask_sqlalchemy
3
4import flask_restless
5
6# Create the Flask application and the Flask-SQLAlchemy object.
7app = flask.Flask(__name__)
8app.config['DEBUG'] = True
9app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
10db = flask_sqlalchemy.SQLAlchemy(app)
11
12
13# Create your Flask-SQLAlchemy models as usual but with the following
14# restriction: they must have an __init__ method that accepts keyword
15# arguments for all columns (the constructor in
16# flask.ext.sqlalchemy.SQLAlchemy.Model supplies such a method, so you
17# don't need to declare a new one).
18class Person(db.Model):
19 id = db.Column(db.Integer, primary_key=True)
20 name = db.Column(db.Unicode)
21 birth_date = db.Column(db.Date)
22
23
24class Article(db.Model):
25 id = db.Column(db.Integer, primary_key=True)
26 title = db.Column(db.Unicode)
27 published_at = db.Column(db.DateTime)
28 author_id = db.Column(db.Integer, db.ForeignKey('person.id'))
29 author = db.relationship(Person, backref=db.backref('articles', lazy='dynamic'))
30
31
32# Create the database tables.
33db.create_all()
34
35# Create the Flask-Restless API manager.
36manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
37
38# Create API endpoints, which will be available at /api/<tablename> by
39# default. Allowed HTTP methods can be specified as well.
40manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])
41manager.create_api(Article, methods=['GET'])
42
43# start the flask loop
44app.run()
You may find this example at examples/quickstart.py
in the source
distribution; you may also view it online. Further examples can be found in
the examples/
directory in the source distribution or on the web