Hi,
I am writing a small app using Flask-WTF and Flask-SQLAlchemy.
Than i try to access database from form a get: RuntimeError: application not
registered on db instance and no application bound to current context
I have:
== extensions.py
from flaskext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
== views.py
from flask import Blueprint
from test1.forms import SampleForm
sample1 = Blueprint('sample1', __name__)
@sample1.route('/')
def index():
# if i import SampleForm here everything works fine, but looks ugly :)
return 'Hello World!'
== alchemy.py
from sqlalchemy.sql import text
from test1.extensions import db
def get_data():
s = text("""
select
id, value
from
some_table
""")
return db.session.execute(s).fetchall()
== forms.py
from flaskext.wtf import Form, SelectField
from test1.alchemy import get_all_sell_points
class SampleForm(Form):
some_data = SelectField(u'Pasirinkite miestą',
choices=[(d.id, d.value) for d in get_data()],
validators=[validators.Required()])
As i understand importing form at the "top" don't passes request context to
it and importing from view works fine because it has context.
So what should i do to make it work and what is the right way to do it?
Regards,
Anton
Hi, On 7/27/11 7:20 PM, Anton wrote: > from flaskext.wtf import Form, SelectField > > from test1.alchemy import get_all_sell_points > > class SampleForm(Form): > some_data = SelectField(u'Pasirinkite miestą', > choices=[(d.id <http://d.id>, d.value) for d in get_data()], > validators=[validators.Required()]) > > > As i understand importing form at the "top" don't passes request context > to it and importing from view works fine because it has context. > So what should i do to make it work and what is the right way to do it? Well. You want your choices to be pulled when the form is used, not when the form is declared. So even if you had database access at that point it would not solve your problem. The simple solution is to set the choices on form __init__ time: from test1.alchemy import get_all_sell_points class SampleForm(Form): some_data = SelectField(u'Pasirinkite miestą', validators=[validators.Required()]) def __init__(self, *args, **kwargs): Form.__init__(self, *args, **kwargs) self.some_data.choices = [(d.id <http://d.id>, d.value) for d in get_data()] Regards, Armin
Hi, Not sure where the '<http://d.id>' came from, remove that of course. Regards, Armin
@Ron: thanks for detailed explanation @Armin: an example i was hoping for, easy and efficient This totally answered all my question. Thanks guys! 2011/7/28 Armin Ronacher <armin.ronacher@active-4.com> > Hi, > > Not sure where the '<http://d.id>' came from, remove that of course. > > > Regards, > Armin >
Hi Anton, On Wed, Jul 27, 2011 at 1:20 PM, Anton <anton.ponadiozin@gmail.com> wrote: > I am writing a small app using Flask-WTF and Flask-SQLAlchemy. > Than i try to access database from form a get: RuntimeError: application not > registered on db instance and no application bound to current context [snip] > == forms.py > from flaskext.wtf import Form, SelectField > from test1.alchemy import get_all_sell_points > class SampleForm(Form): > some_data = SelectField(u'Pasirinkite miestą', > choices=[(d.id, d.value) for d in get_data()], > validators=[validators.Required()]) Right here, in the choices keyword, you are running a query when this class is declared. Try instead, a QuerySelectField: http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.sqlalchemy.fields -Ron
I got same error: RuntimeError: application not registered on db instance
and no application bound to current context
== forms.py
# -*- coding: utf-8 -*-
from flaskext.wtf import Form, SelectField, validators, DateTimeField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from test1.alchemy import get_data
class SampleForm(Form):
city = QuerySelectField(u'Choose city',
query_factory=[(d.id, d.city) for d in get_data()],
validators=[validators.Required()])
2011/7/27 Ron DuPlain <ron.duplain@gmail.com>
> Hi Anton,
>
> On Wed, Jul 27, 2011 at 1:20 PM, Anton <anton.ponadiozin@gmail.com> wrote:
> > I am writing a small app using Flask-WTF and Flask-SQLAlchemy.
> > Than i try to access database from form a get: RuntimeError: application
> not
> > registered on db instance and no application bound to current context
> [snip]
> > == forms.py
> > from flaskext.wtf import Form, SelectField
> > from test1.alchemy import get_all_sell_points
> > class SampleForm(Form):
> > some_data = SelectField(u'Pasirinkite miestą',
> > choices=[(d.id, d.value) for d in get_data()],
> > validators=[validators.Required()])
>
> Right here, in the choices keyword, you are running a query when this
> class is declared. Try instead, a QuerySelectField:
>
>
>
http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.sqlalchemy.fields
>
> -Ron
>
Hi Anton, On Thu, Jul 28, 2011 at 7:38 AM, Anton <anton.ponadiozin@gmail.com> wrote: > I got same error: RuntimeError: application not registered on db instance > and no application bound to current context > == forms.py > # -*- coding: utf-8 -*- > from flaskext.wtf import Form, SelectField, validators, DateTimeField > from wtforms.ext.sqlalchemy.fields import QuerySelectField > from test1.alchemy import get_data > class SampleForm(Form): > city = QuerySelectField(u'Choose city', > query_factory=[(d.id, d.city) for d in get_data()], > validators=[validators.Required()]) query_factory needs to return a SQLAlchemy query object, not a list. http://www.sqlalchemy.org/docs/orm/query.html?highlight=query#sqlalchemy.orm.query.Query To understand the error: Your code is creating the form class with a query, and queries with Flask-SQLAlchemy requires a Flask app in context so that it knows where & how to connect to your database. Another important reason, since you are building options from the database, you want these options to populate on request, not app initialization. The approach then is to provide a function that provides either a SQLAlchemy query object (QuerySelectField) or a list (SelectField). Either way, this needs to be called at the time of request. Does that clarify the error? Ron > 2011/7/27 Ron DuPlain <ron.duplain@gmail.com> >> >> Hi Anton, >> >> On Wed, Jul 27, 2011 at 1:20 PM, Anton <anton.ponadiozin@gmail.com> wrote: >> > I am writing a small app using Flask-WTF and Flask-SQLAlchemy. >> > Than i try to access database from form a get: RuntimeError: application >> > not >> > registered on db instance and no application bound to current context >> [snip] >> > == forms.py >> > from flaskext.wtf import Form, SelectField >> > from test1.alchemy import get_all_sell_points >> > class SampleForm(Form): >> > some_data = SelectField(u'Pasirinkite miestą', >> > choices=[(d.id, d.value) for d in get_data()], >> > validators=[validators.Required()]) >> >> Right here, in the choices keyword, you are running a query when this >> class is declared. Try instead, a QuerySelectField: >> >> >> http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.sqlalchemy.fields >> >> -Ron > >