Skip to content

Instantly share code, notes, and snippets.

@mtiller
Created February 16, 2013 12:40
Show Gist options
  • Save mtiller/4966755 to your computer and use it in GitHub Desktop.
Save mtiller/4966755 to your computer and use it in GitHub Desktop.
How this could be done...
from flask import Flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.mongorest import MongoRest
from flask.ext.mongorest.views import ResourceView
from flask.ext.mongorest.resources import Resource
from flask.ext.mongorest import operators as ops
from flask.ext.mongorest import methods
app = Flask(__name__)
// Configure your database here
db = MongoEngine(app)
api = MongoRest(app)
class Author(db.Document):
name = db.StringField(required=True)
class AuthorResource(Resource):
document = Author
uri_path = '/authors/'
class Book(db.Document):
title = db.StringField(required=True)
author = db.ReferenceField(Author, dbref=False)
class BookResource(Resource):
document = Book
related_resources = {'author': AuthorResource}
uri_path = '/books/'
@api.register(name='authors', url=AuthorResource.uri_path)
class AuthorView(ResourceView):
resource = AuthorResource
methods = [methods.Create, methods.Update, methods.Fetch, methods.List]
@api.register(name='books', url=BookResource.uri_path)
class AuthorView(ResourceView):
resource = BookResource
methods = [methods.Create, methods.Update, methods.Fetch, methods.List]
if __name__=='__main__':
app.debug = True
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment