librelist archives

« back to archive

Parameters for the instantiation of flask.ext.restful.Resource

Parameters for the instantiation of flask.ext.restful.Resource

Rainer Poisel
2013-07-14 @ 13:24
Dear Flask community,

I am currently implementing a somewhat MVC-like application layout with
(RESTful) Flask. Therefore I created the following construct:

from flask import Flask
from flask.ext.restful import Api, Resource

def main():
    lModel = RangeManager()
    lApp = Flask(__name__)
    lApi = Api(lApp)

    FetchRange.sModel = lModel
    lApi.add_resource(FetchRange, '/fetch')

    # start server
    # omitted to keep the example short and tidy

class FetchRange(Resource):
     def get(self):
        return FetchRange.sModel.getRange()

As you can see, access to the model (implemented by me) is accomplished
through a class-variable (FetchRange.sModel). Is there a better way to
provide object refs to Resources to be used during their instantiation
(managed by Flask-RESTful) or during object-lifetime? I do not want to work
with global variables.

Thanks in advance,