librelist archives

Using Flask-SQLAlchemy within a class

Using Flask-SQLAlchemy within a class

From:

Kerem Ulutaş

Date:

2012-08-10 @ 23:37

Hi all,
In my previous post I asked for a way to use app instance in a class,
according to recommendations here is an example of what I have:
===============================================
from flask import Flask
from flask.views import View
class RenderTemplateView(View):
def __init__(self, template_name, methods=['GET']):
self.template_name = template_name
self.methods = methods
def dispatch_request(self):
try:
return render_template(self.template_name)
except Exception as e:
self.logger.exception(e)
class MyApp(object):
def __init__(self):
self.app = Flask(__name__)
self.app.add_url_rule("/",
view_func=RenderTemplateView.as_view('index',
template_name='index.html'))
myappinstance = MyApp()
app = myappinstance.app
===============================================
Now, what I'm trying to accomplish is to use Flask-SQLAlchemy, I came
accross an example (
http://packages.python.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application)
when
following the docs.
What is the best way to implement it with the code I have? As you can see,
I'm trying to follow an object oriented approach. I have been able to
create a class with the help of flask.views.View which I could bind URL's
by calling self.app.add_url_rule - but I couldn't do the same with
Flask-SQLAlchemy.
What would be the most appropriate and most Pythonic way?
-- Blog'umu okudunuz mu? http://www.ulutas.gen.trThe box said "Requires Windows 95, NT, or better", so I installed Linux.