Created
July 15, 2016 05:45
-
-
Save nenodias/ed55528bdc04124ac22c2e8587104e09 to your computer and use it in GitHub Desktop.
Flask-restless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import flask.ext.sqlalchemy | |
app = flask.Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' | |
db = flask.ext.sqlalchemy.SQLAlchemy(app) | |
class Person(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.Unicode, unique=True) | |
birth_date = db.Column(db.Date) | |
computers = db.relationship('Computer', uselist=False, back_populates="owner") | |
class Computer(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.Unicode, unique=True) | |
vendor = db.Column(db.Unicode) | |
owner = db.relationship('Person', uselist=False, back_populates="computers") | |
owner_id = db.Column(db.Integer, db.ForeignKey('person.id')) | |
purchase_time = db.Column(db.DateTime) | |
db.create_all() | |
import flask.ext.restless | |
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db) | |
print(manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])) | |
manager.create_api(Person, methods=['GET', 'POST', 'DELETE']) | |
manager.create_api(Computer) | |
person_blueprint = manager.create_api_blueprint(Person, methods=['GET', 'POST']) | |
computer_blueprint = manager.create_api_blueprint(Computer, methods=['GET', 'POST']) | |
app.register_blueprint(person_blueprint) | |
app.register_blueprint(computer_blueprint) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment