librelist archives

« back to archive

Using Flask-SQLAlchemy within a class

Using Flask-SQLAlchemy within a class

Kerem Ulutaş
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):
            return render_template(self.template_name)
        except Exception as e:

class MyApp(object):
    def __init__(self): = Flask(__name__)"/",

myappinstance = MyApp()
app =

Now, what I'm trying to accomplish is to use Flask-SQLAlchemy, I came
accross an example (
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 - but I couldn't do the same with
What would be the most appropriate and most Pythonic way?


Blog'umu okudunuz mu?

The box said "Requires Windows 95, NT, or better", so I installed Linux.