librelist archives

« back to archive

RuntimeError Application not registered on db and no application context

RuntimeError Application not registered on db and no application context

From:
Blake Adams
Date:
2014-10-23 @ 13:00
My app is built around the 'main' blueprint.   I created a helper.py within
'main' to use a function for my form.py file to dynamically generate list
from a model to be used in a SelectField.  I keep getting the 'app not
registered on db" error.

Main parts of my file structure are:


   - myproject
      - app/
         - __init__.py
         - admin/
         - main/
            - __init__.py
            - forms.py
            - routes.py
            - helper.py
         - models.py
         - forms.py
         - static/
         - templates/
      - manage.py
      - config.py

I originally had a hard coded choice list for a Select field in forms.py -
everything worked fine.  To make things dynamics I created helper.py below:

*# helper.py*
from . import main
from ..models import Model1, Model2
from app import db

def get_categories():
    cat_list = []
    for cat in db.session.query(Model1).all():
    cat_list.append((cat.id,cat.category))
    return cat_list

When i run the the 'get_categories' function in the shell works fine but
when i try to use it in forms.py to make dynamic list, app breaks.  Have
done a lot of searching and tried several difference things (went from app
factory to instant app creation but didn't help).  What am I doing wrong
and is there a best practice for where to put helper functions?


Heres the forms.py file:
*# forms.py*
from flask.ext.wtf import Form
from wtforms import StringField, SelectField
from wtforms.fields.html5 import DateField
from wtforms.validators import Required, InputRequired, Length, Email
from .helper import get_categories

class prod_entry(Form):
    ID = StringField('ID',validators= [Required(),Length(max=32)])
    name = StringField('name',validators= [Required(),Length(max=64)])
    brand = StringField('Brand',validators= [Length(max=64)])
    category = SelectField('Category', choices=get_categories(),coerce=int)

Also heres my app/__init__.py file:

#__init__.py

import os
from flask import Flask
from config import config
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()

from flask.ext.bootstrap import Bootstrap

bootstrap = Bootstrap()

app = Flask(__name__)
app.config.from_object(config[os.getenv('FLASK_CONFIG') or 'default'])
bootstrap.init_app(app)
db.init_app(app)

from .main import main as main_blueprint
app.register_blueprint(main_blueprint)


Thanks Much,

Blake

Re: [flask] RuntimeError Application not registered on db and no application context

From:
digwtx
Date:
2014-10-23 @ 14:26
try this:

db.init_app(app)
db.app = app

在 2014-10-23 21:00:20,"Blake Adams" <blakesadams@gmail.com> 写道:

My app is built around the 'main' blueprint.   I created a helper.py 
within 'main' to use a function for my form.py file to dynamically 
generate list from a model to be used in a SelectField.  I keep getting 
the 'app not registered on db" error.  


Main parts of my file structure are:


myproject
app/
__init__.py
admin/
main/
__init__.py
forms.py
routes.py
helper.py
models.py
forms.py
static/
templates/
manage.py
config.py
I originally had a hard coded choice list for a Select field in forms.py -
everything worked fine.  To make things dynamics I created helper.py 
below:


# helper.py
from . import main
from ..models import Model1, Model2
from app import db


def get_categories():
    cat_list = []
    for cat in db.session.query(Model1).all():
   cat_list.append((cat.id,cat.category))
    return cat_list


When i run the the 'get_categories' function in the shell works fine but 
when i try to use it in forms.py to make dynamic list, app breaks.  Have 
done a lot of searching and tried several difference things (went from app
factory to instant app creation but didn't help).  What am I doing wrong 
and is there a best practice for where to put helper functions?




Heres the forms.py file:
# forms.py
from flask.ext.wtf import Form
from wtforms import StringField, SelectField
from wtforms.fields.html5 import DateField
from wtforms.validators import Required, InputRequir ed, Length, Email
from .helper import get_categories


class prod_entry(Form):
    ID = StringField('ID',validators= [Required(),Length(max=32)])
    name = StringField('name',validators= [Required(),Length(max=64)])
    brand = StringField('Brand',validators= [Length(max=64)])
    category = SelectField('Category', choices=get_categories(),coerce=int)


Also heres my app/__init__.py file:


#__init__.py


import os
from flask import Flask
from config import config
from flask.ext.sqlalchemy import SQLAlchemy


db = SQLAlchemy()


from flask.ext.bootstrap import Bootstrap


bootstrap = Bootstrap()


app = Flask(__name__)
app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
bootstrap.init_app(app)
db.init_app(app)


from .main import main as main_blueprint
app.register_blueprint(main_blueprint)




Thanks Much,


Blake

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Eric B
Date:
2014-10-23 @ 13:18
In your imports i see: InputRequir ed

Is that how it looks in the app?

What's the error it is throwing you?
On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:

> My app is built around the 'main' blueprint.   I created a helper.py
> within 'main' to use a function for my form.py file to dynamically generate
> list from a model to be used in a SelectField.  I keep getting the 'app not
> registered on db" error.
>
> Main parts of my file structure are:
>
>
>    - myproject
>       - app/
>          - __init__.py
>          - admin/
>          - main/
>             - __init__.py
>             - forms.py
>             - routes.py
>             - helper.py
>          - models.py
>          - forms.py
>          - static/
>          - templates/
>       - manage.py
>       - config.py
>
> I originally had a hard coded choice list for a Select field in forms.py -
> everything worked fine.  To make things dynamics I created helper.py below:
>
> *# helper.py*
> from . import main
> from ..models import Model1, Model2
> from app import db
>
> def get_categories():
>     cat_list = []
>     for cat in db.session.query(Model1).all():
>     cat_list.append((cat.id,cat.category))
>     return cat_list
>
> When i run the the 'get_categories' function in the shell works fine but
> when i try to use it in forms.py to make dynamic list, app breaks.  Have
> done a lot of searching and tried several difference things (went from app
> factory to instant app creation but didn't help).  What am I doing wrong
> and is there a best practice for where to put helper functions?
>
>
> Heres the forms.py file:
> *# forms.py*
> from flask.ext.wtf import Form
> from wtforms import StringField, SelectField
> from wtforms.fields.html5 import DateField
> from wtforms.validators import Required, InputRequir ed, Length, Email
> from .helper import get_categories
>
> class prod_entry(Form):
>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>     name = StringField('name',validators= [Required(),Length(max=64)])
>     brand = StringField('Brand',validators= [Length(max=64)])
>     category = SelectField('Category', choices=get_categories(),coerce=int)
>
> Also heres my app/__init__.py file:
>
> #__init__.py
>
> import os
> from flask import Flask
> from config import config
> from flask.ext.sqlalchemy import SQLAlchemy
>
> db = SQLAlchemy()
>
> from flask.ext.bootstrap import Bootstrap
>
> bootstrap = Bootstrap()
>
> app = Flask(__name__)
> app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
> bootstrap.init_app(app)
> db.init_app(app)
>
> from .main import main as main_blueprint
> app.register_blueprint(main_blueprint)
>
>
> Thanks Much,
>
> Blake
>

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Blake Adams
Date:
2014-10-23 @ 13:29
You can ignore the inputRequired - it was used ii another field that i
cutout for expediency.

Full stacktrace below:

Traceback (most recent call last):

  File "manage.py", line 8, in <module>

    from app import app, models, db

  File "/Users/myfolder/Dev/myapp/app/__init__.py", line 17, in <module>

    from .main import main as main_blueprint

  File "/Users/myfolder/Dev/myapp/app/main/__init__.py", line 4, in <module>

    from . import routes, helper, forms

  File "/Users/myfolder/Dev/myapp/app/main/routes.py", line 5, in <module>

    from .forms import prod_entry, sku_list_search

  File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 7, in <module>

    class prod_entry(Form):

  File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 11, in prod_entry

    category = SelectField('Category', choices=get_categories(),coerce=int)

  File "/Users/myfolder/Dev/myapp/app/main/helper.py", line 8, in
get_categories

    for cat in db.session.query(Category).all():

  File

"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/orm/scoping.py",
line 150, in do

    return getattr(self.registry(), name)(*args, **kwargs)

  File

"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/util/_collections.py",
line 878, in __call__

    return self.registry.setdefault(key, self.createfunc())

  File

"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
line 704, in create_session

    return SignallingSession(self, **options)

  File

"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
line 149, in __init__

    self.app = db.get_app()

  File

"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
line 845, in get_app

    raise RuntimeError('application not registered on db '

RuntimeError: application not registered on db instance and no application
bound to current context

On Thu, Oct 23, 2014 at 9:18 AM, Eric B <neurosnap@gmail.com> wrote:

> In your imports i see: InputRequir ed
>
> Is that how it looks in the app?
>
> What's the error it is throwing you?
> On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>
>> My app is built around the 'main' blueprint.   I created a helper.py
>> within 'main' to use a function for my form.py file to dynamically generate
>> list from a model to be used in a SelectField.  I keep getting the 'app not
>> registered on db" error.
>>
>> Main parts of my file structure are:
>>
>>
>>    - myproject
>>       - app/
>>          - __init__.py
>>          - admin/
>>          - main/
>>             - __init__.py
>>             - forms.py
>>             - routes.py
>>             - helper.py
>>          - models.py
>>          - forms.py
>>          - static/
>>          - templates/
>>       - manage.py
>>       - config.py
>>
>> I originally had a hard coded choice list for a Select field in forms.py
>> - everything worked fine.  To make things dynamics I created helper.py
>> below:
>>
>> *# helper.py*
>> from . import main
>> from ..models import Model1, Model2
>> from app import db
>>
>> def get_categories():
>>     cat_list = []
>>     for cat in db.session.query(Model1).all():
>>     cat_list.append((cat.id,cat.category))
>>     return cat_list
>>
>> When i run the the 'get_categories' function in the shell works fine but
>> when i try to use it in forms.py to make dynamic list, app breaks.  Have
>> done a lot of searching and tried several difference things (went from app
>> factory to instant app creation but didn't help).  What am I doing wrong
>> and is there a best practice for where to put helper functions?
>>
>>
>> Heres the forms.py file:
>> *# forms.py*
>> from flask.ext.wtf import Form
>> from wtforms import StringField, SelectField
>> from wtforms.fields.html5 import DateField
>> from wtforms.validators import Required, InputRequir ed, Length, Email
>> from .helper import get_categories
>>
>> class prod_entry(Form):
>>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>>     name = StringField('name',validators= [Required(),Length(max=64)])
>>     brand = StringField('Brand',validators= [Length(max=64)])
>>     category = SelectField('Category',
>> choices=get_categories(),coerce=int)
>>
>> Also heres my app/__init__.py file:
>>
>> #__init__.py
>>
>> import os
>> from flask import Flask
>> from config import config
>> from flask.ext.sqlalchemy import SQLAlchemy
>>
>> db = SQLAlchemy()
>>
>> from flask.ext.bootstrap import Bootstrap
>>
>> bootstrap = Bootstrap()
>>
>> app = Flask(__name__)
>> app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
>> bootstrap.init_app(app)
>> db.init_app(app)
>>
>> from .main import main as main_blueprint
>> app.register_blueprint(main_blueprint)
>>
>>
>> Thanks Much,
>>
>> Blake
>>
>

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Eric B
Date:
2014-10-23 @ 13:51
I don't know for sure but It looks like SQLAlchemy doesn't know which app
context to use.


http://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w

http://flask.pocoo.org/docs/0.10/appcontext/

However, it doesn't look like you are using a "create_app" method so
db.init_app(app) seems pointless?  Either create the proper app function (
http://flask.pocoo.org/snippets/20/) and select the proper app context for
SQLalchemy as suggested in the above SO post or remove db.init_app(app) and
replace it with db = SQLAlchemy(app).

On Thu, Oct 23, 2014 at 9:29 AM, Blake Adams <blakesadams@gmail.com> wrote:

> You can ignore the inputRequired - it was used ii another field that i
> cutout for expediency.
>
> Full stacktrace below:
>
> Traceback (most recent call last):
>
>   File "manage.py", line 8, in <module>
>
>     from app import app, models, db
>
>   File "/Users/myfolder/Dev/myapp/app/__init__.py", line 17, in <module>
>
>     from .main import main as main_blueprint
>
>   File "/Users/myfolder/Dev/myapp/app/main/__init__.py", line 4, in
> <module>
>
>     from . import routes, helper, forms
>
>   File "/Users/myfolder/Dev/myapp/app/main/routes.py", line 5, in <module>
>
>     from .forms import prod_entry, sku_list_search
>
>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 7, in <module>
>
>     class prod_entry(Form):
>
>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 11, in
> prod_entry
>
>     category = SelectField('Category', choices=get_categories(),coerce=int)
>
>   File "/Users/myfolder/Dev/myapp/app/main/helper.py", line 8, in
> get_categories
>
>     for cat in db.session.query(Category).all():
>
>   File
> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/orm/scoping.py",
> line 150, in do
>
>     return getattr(self.registry(), name)(*args, **kwargs)
>
>   File
> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/util/_collections.py",
> line 878, in __call__
>
>     return self.registry.setdefault(key, self.createfunc())
>
>   File
> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
> line 704, in create_session
>
>     return SignallingSession(self, **options)
>
>   File
> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
> line 149, in __init__
>
>     self.app = db.get_app()
>
>   File
> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
> line 845, in get_app
>
>     raise RuntimeError('application not registered on db '
>
> RuntimeError: application not registered on db instance and no application
> bound to current context
>
> On Thu, Oct 23, 2014 at 9:18 AM, Eric B <neurosnap@gmail.com> wrote:
>
>> In your imports i see: InputRequir ed
>>
>> Is that how it looks in the app?
>>
>> What's the error it is throwing you?
>> On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>>
>>> My app is built around the 'main' blueprint.   I created a helper.py
>>> within 'main' to use a function for my form.py file to dynamically generate
>>> list from a model to be used in a SelectField.  I keep getting the 'app not
>>> registered on db" error.
>>>
>>> Main parts of my file structure are:
>>>
>>>
>>>    - myproject
>>>       - app/
>>>          - __init__.py
>>>          - admin/
>>>          - main/
>>>             - __init__.py
>>>             - forms.py
>>>             - routes.py
>>>             - helper.py
>>>          - models.py
>>>          - forms.py
>>>          - static/
>>>          - templates/
>>>       - manage.py
>>>       - config.py
>>>
>>> I originally had a hard coded ch oice list for a Select field in
>>> forms.py - everything worked fine.  To make things dynamics I created
>>> helper.py below:
>>>
>>> *# helper.py*
>>> from . import main
>>> from ..models import Model1, Model2
>>> from app import db
>>>
>>> def get_categories():
>>>     cat_list = []
>>>     for cat in db.session.query(Model1).all():
>>>     cat_list.append((cat.id,cat.category))
>>>     return cat_list
>>>
>>> When i run the the 'get_categories' function in the shell works fine but
>>> when i try to use it in forms.py to make dynamic list, app breaks.  Have
>>> done a lot of searching and tried several difference things (went from app
>>> factory to instant app creation but didn't help).  What am I doing wrong
>>> and is there a best practice for where to put helper functions?
>>>
>>>
>>> Heres the forms.py file:
>>> *# forms.py*
>>> from flask.ext.wtf import Form
>>> from wtforms import StringField, SelectField
>>> from wtforms.fields.html5 import DateField
>>> from wtforms.validators import Required, InputRequir ed, Length, Email
>>> from .helper import get_categories
>>>
>>> class prod_entry(Form):
>>>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>>>     name = StringField('name',validators= [Required(),Length(max=64)])
>>>     brand = StringField('Brand',validators= [Length(max=64)])
>>>     category = SelectField('Category',
>>> choices=get_categories(),coerce=int)
>>>
>>> Also heres my app/__init__.py file:
>>>
>>> #__init__.py
>>>
>>> import os
>>> from flask import Flask
>>> from config import config
>>> from flask.ext.sqlalchemy import SQLAlchemy
>>>
>>> db = SQLAlchemy()
>>>
>>> from flask.ext.bootstrap import Bootstrap
>>>
>>> bootstrap = Bootstrap()
>>>
>>> app = Flask(__name__)
>>> app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
>>> bootstrap.init_app(app)
>>> db.init_app(app)
>>>
>>> from .main import main as main_blueprint
>>> app.register_blueprint(main_blueprint)
>>>
>>>
>>> Thanks Much,
>>>
>>> Blake
>>>
>>
>

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Blake Adams
Date:
2014-10-23 @ 14:19
Eric, you right -  looks like when I removed the app factory I also needed
to change the db initialization to db = SQLAlchemy(app) as you described
Thanks!

For future reference, when using the create_app method, do you know how
when I should specify the proper app for SQLalchemy?  Would I set a context
in the function within helper.py?







On Thu, Oct 23, 2014 at 9:51 AM, Eric B <neurosnap@gmail.com> wrote:

> I don't know for sure but It looks like SQLAlchemy doesn't know which app
> context to use.
>
>
> 
http://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w
>
> http://flask.pocoo.org/docs/0.10/appcontext/
>
> However, it doesn't look like you are using a "create_app" method so
> db.init_app(app) seems pointless?  Either create the proper app function (
> http://flask.pocoo.org/snippets/20/) and select the proper app context
> for SQLalchemy as suggested in the above SO post or remove db.init_app(app)
> and replace it with db = SQLAlchemy(app).
>
> On Thu, Oct 23, 2014 at 9:29 AM, Blake Adams <blakesadams@gmail.com>
> wrote:
>
>> You can ignore the inputRequired - it was used ii another field that i
>> cutout for expediency.
>>
>> Full stacktrace below:
>>
>> Traceback (most recent call last):
>>
>>   File "manage.py", line 8, in <module>
>>
>>     from app import app, models, db
>>
>>   File "/Users/myfolder/Dev/myapp/app/__init__.py", line 17, in <module>
>>
>>     from .main import main as main_blueprint
>>
>>   File "/Users/myfolder/Dev/myapp/app/main/__init__.py", line 4, in
>> <module>
>>
>>     from . import routes, helper, forms
>>
>>   File "/Users/myfolder/Dev/myapp/app/main/routes.py", line 5, in <module>
>>
>>     from .forms import prod_entry, sku_list_search
>>
>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 7, in <module>
>>
>>     class prod_entry(Form):
>>
>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 11, in
>> prod_entry
>>
>>     category = SelectField('Category',
>> choices=get_categories(),coerce=int)
>>
>>   File "/Users/myfolder/Dev/myapp/app/main/helper.py", line 8, in
>> get_categories
>>
>>     for cat in db.session.query(Category).all():
>>
>>   File
>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/orm/scoping.py",
>> line 150, in do
>>
>>     return getattr(self.registry(), name)(*args, **kwargs)
>>
>>   File
>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/util/_collections.py",
>> line 878, in __call__
>>
>>     return self.registry.setdefault(key, self.createfunc())
>>
>>   File
>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>> line 704, in create_session
>>
>>     return SignallingSession(self, **options)
>>
>>   File
>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>> line 149, in __init__
>>
>>     self.app = db.get_app()
>>
>>   File
>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>> line 845, in get_app
>>
>>     raise RuntimeError('application not registered on db '
>>
>> RuntimeError: application not registered on db instance and no
>> application bound to current context
>>
>> On Thu, Oct 23, 2014 at 9:18 AM, Eric B <neurosnap@gmail.com> wrote:
>>
>>> In your imports i see: InputRequir ed
>>>
>>> Is that how it looks in the app?
>>>
>>> What's the error it is throwing you?
>>> On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>>>
>>>> My app is built around the 'main' blueprint.   I created a helper.py
>>>> within 'main' to use a function for my form.py file to dynamically generate
>>>> list from a model to be used in a SelectField.  I keep getting the 'app not
>>>> registered on db" error.
>>>>
>>>> Main parts of my file structure are:
>>>>
>>>>
>>>>    - myproject
>>>>       - app/
>>>>          - __init__.py
>>>>          - admin/
>>>>          - main/
>>>>             - __init__.py
>>>>             - forms.py
>>>>             - routes.py
>>>>             - helper.py
>>>>          - models.py
>>>>          - forms.py
>>>>          - static/
>>>>          - templates/
>>>>       - manage.py
>>>>       - config.py
>>>>
>>>> I originally had a hard coded ch oice list for a Select field in
>>>> forms.py - everything worked fine.  To make things dynamics I created
>>>> helper.py below:
>>>>
>>>> *# helper.py*
>>>> from . import main
>>>> from ..models import Model1, Model2
>>>> from app import db
>>>>
>>>> def get_categories():
>>>>     cat_list = []
>>>>     for cat in db.session.query(Model1).all():
>>>>     cat_list.append((cat.id,cat.category))
>>>>     return cat_list
>>>>
>>>> When i run the the 'get_categories' function in the shell works fine
>>>> but when i try to use it in forms.py to make dynamic list, app breaks.
>>>> Have done a lot of searching and tried several difference things (went from
>>>> app factory to instant app creation but didn't help).  What am I doing
>>>> wrong and is there a best practice for where to put helper functions?
>>>>
>>>>
>>>> Heres the forms.py file:
>>>> *# forms.py*
>>>> from flask.ext.wtf import Form
>>>> from wtforms import StringField, SelectField
>>>> from wtforms.fields.html5 import DateField
>>>> from wtforms.validators import Required, InputRequir ed, Length, Email
>>>> from .helper import get_categories
>>>>
>>>> class prod_entry(Form):
>>>>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>>>>     name = StringField('name',validators= [Required(),Length(max=64)])
>>>>     brand = StringField('Brand',validators= [Length(max=64)])
>>>>     category = SelectField('Category',
>>>> choices=get_categories(),coerce=int)
>>>>
>>>> Also heres my app/__init__.py file:
>>>>
>>>> #__init__.py
>>>>
>>>> import os
>>>> from flask import Flask
>>>> from config import config
>>>> from flask.ext.sqlalchemy import SQLAlchemy
>>>>
>>>> db = SQLAlchemy()
>>>>
>>>> from flask.ext.bootstrap import Bootstrap
>>>>
>>>> bootstrap = Bootstrap()
>>>>
>>>> app = Flask(__name__)
>>>> app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
>>>> bootstrap.init_app(app)
>>>> db.init_app(app)
>>>>
>>>> from .main import main as main_blueprint
>>>> app.register_blueprint(main_blueprint)
>>>>
>>>>
>>>> Thanks Much,
>>>>
>>>> Blake
>>>>
>>>
>>
>

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Eric B
Date:
2014-10-23 @ 14:25
What I usually do is db.init_app(app) within create_app and do db =
SQLAlchemy () within my models.py then you import db from models.py
 On Oct 23, 2014 10:22 AM, "Blake Adams" <blakesadams@gmail.com> wrote:

> Eric, you right -  looks like when I removed the app factory I also needed
> to change the db initialization to db = SQLAlchemy(app) as you described
> Thanks!
>
> For future reference, when using the create_app method, do you know how
> when I should specify the proper app for SQLalchemy?  Would I set a context
> in the function within helper.py?
>
>
>
>
>
>
>
> On Thu, Oct 23, 2014 at 9:51 AM, Eric B <neurosnap@gmail.com> wrote:
>
>> I don't know for sure but It looks like SQLAlchemy doesn't know which app
>> context to use.
>>
>>
>> 
http://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w
>>
>> http://flask.pocoo.org/docs/0.10/appcontext/
>>
>> However, it doesn't look like you are using a "create_app" method so
>> db.init_app(app) seems pointless?  Either create the proper app function (
>> http://flask.pocoo.org/snippets/20/) and select the proper app context
>> for SQLalchemy as suggested in the above SO post or remove db.init_app(app)
>> and replace it with db = SQLAlchemy(app).
>>
>> On Thu, Oct 23, 2014 at 9:29 AM, Blake Adams <blakesadams@gmail.com>
>> wrote:
>>
>>> You can ignore the inputRequired - it was used ii another field that i
>>> cutout for expediency.
>>>
>>> Full stacktrace below:
>>>
>>> Traceback (most recent call last):
>>>
>>>   File "manage.py", line 8, in <module>
>>>
>>>     from app import app, models, db
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/__init__.py", line 17, in <module>
>>>
>>>     from .main import main as main_blueprint
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/main/__init__.py", line 4, in
>>> <module>
>>>
>>>     from . import routes, helper, forms
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/main/routes.py", line 5, in
>>> <module>
>>>
>>>     from .forms import prod_entry, sku_list_search
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 7, in <module>
>>>
>>>     class prod_entry(Form):
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 11, in
>>> prod_entry
>>>
>>>     category = SelectField('Category',
>>> choices=get_categories(),coerce=int)
>>>
>>>   File "/Users/myfolder/Dev/myapp/app/main/helper.py", line 8, in
>>> get_categories
>>>
>>>     for cat in db.session.query(Category).all():
>>>
>>>   File
>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/orm/scoping.py",
>>> line 150, in do
>>>
>>>     return getattr(self.registry(), name)(*args, **kwargs)
>>>
>>>   File
>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/util/_collections.py",
>>> line 878, in __call__
>>>
>>>     return self.registry.setdefault(key, self.createfunc())
>>>
>>>   File
>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>> line 704, in create_session
>>>
>>>     return SignallingSession(self, **options)
>>>
>>>   File
>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>> line 149, in __init__
>>>
>>>     self.app = db.get_app()
>>>
>>>   File
>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>> line 845, in get_app
>>>
>>>     raise RuntimeError('application not registered on db '
>>>
>>> RuntimeError: application not registered on db instance and no
>>> application bound to current context
>>>
>>> On Thu, Oct 23, 2014 at 9:18 AM, Eric B <neurosnap@gmail.com> wrote:
>>>
>>>> In your imports i see: InputRequir ed
>>>>
>>>> Is that how it looks in the app?
>>>>
>>>> What's the error it is throwing you?
>>>> On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>>>>
>>>>> My app is built around the 'main' blueprint.   I created a helper.py
>>>>> within 'main' to use a function for my form.py file to dynamically generate
>>>>> list from a model to be used in a SelectField.  I keep getting the 'app not
>>>>> registered on db" error.
>>>>>
>>>>> Main parts of my file structure are:
>>>>>
>>>>>
>>>>>    - myproject
>>>>>       - app/
>>>>>          - __init__.py
>>>>>          - admin/
>>>>>          - main/
>>>>>             - __init__.py
>>>>>             - forms.py
>>>>>             - routes.py
>>>>>             - helper.py
>>>>>          - models.py
>>>>>          - forms.py
>>>>>          - static/
>>>>>          - templates/
>>>>>       - manage.py
>>>>>       - config.py
>>>>>
>>>>> I originally had a hard coded ch oice list for a Select field in
>>>>> forms.py - everything worked fine.  To make things dynamics I created
>>>>> helper.py below:
>>>>>
>>>>> *# helper.py*
>>>>> from . import main
>>>>> from ..models import Model1, Model2
>>>>> from app import db
>>>>>
>>>>> def get_categories():
>>>>>     cat_list = []
>>>>>     for cat in db.session.query(Model1).all():
>>>>>     cat_list.append((cat.id,cat.category))
>>>>>     return cat_list
>>>>>
>>>>> When i run the the 'get_categories' function in the shell works fine
>>>>> but when i try to use it in forms.py to make dynamic list, app breaks.
>>>>> Have done a lot of searching and tried several difference things (went from
>>>>> app factory to instant app creation but didn't help).  What am I doing
>>>>> wrong and is there a best practice for where to put helper functions?
>>>>>
>>>>>
>>>>> Heres the forms.py file:
>>>>> *# forms.py*
>>>>> from flask.ext.wtf import Form
>>>>> from wtforms import StringField, SelectField
>>>>> from wtforms.fields.html5 import DateField
>>>>> from wtforms.validators import Required, InputRequir ed, Length, Email
>>>>> from .helper import get_categories
>>>>>
>>>>> class prod_entry(Form):
>>>>>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>>>>>     name = StringField('name',validators= [Required(),Length(max=64)])
>>>>>     brand = StringField('Brand',validators= [Length(max=64)])
>>>>>     category = SelectField('Category',
>>>>> choices=get_categories(),coerce=int)
>>>>>
>>>>> Also heres my app/__init__.py file:
>>>>>
>>>>> #__init__.py
>>>>>
>>>>> import os
>>>>> from flask import Flask
>>>>> from config import config
>>>>> from flask.ext.sqlalchemy import SQLAlchemy
>>>>>
>>>>> db = SQLAlchemy()
>>>>>
>>>>> from flask.ext.bootstrap import Bootstrap
>>>>>
>>>>> bootstrap = Bootstrap()
>>>>>
>>>>> app = Flask(__name__)
>>>>> app.config.from_object(config[os.geten v('FLASK_CONFIG') or 'default'])
>>>>> bootstrap.init_app(app)
>>>>> db.init_app(app)
>>>>>
>>>>> from .main import main as main_blueprint
>>>>> app.register_blueprint(main_blueprint)
>>>>>
>>>>>
>>>>> Thanks Much,
>>>>>
>>>>> Blake
>>>>>
>>>>
>>>
>>
>

Re: [flask] RuntimeError Application not registered on db and no application context

From:
Blake Adams
Date:
2014-10-23 @ 14:39
Gotcha thanks a lot

On Thu, Oct 23, 2014 at 10:25 AM, Eric B <neurosnap@gmail.com> wrote:

> What I usually do is db.init_app(app) within create_app and do db =
> SQLAlchemy () within my models.py then you import db from models.py
>  On Oct 23, 2014 10:22 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>
>> Eric, you right -  looks like when I removed the app factory I also
>> needed to change the db initialization to db = SQLAlchemy(app) as you
>> described
>> Thanks!
>>
>> For future reference, when using the create_app method, do you know how
>> when I should specify the proper app for SQLalchemy?  Would I set a context
>> in the function within helper.py?
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Oct 23, 2014 at 9:51 AM, Eric B <neurosnap@gmail.com> wrote:
>>
>>> I don't know for sure but It looks like SQLAlchemy doesn't know which
>>> app context to use.
>>>
>>>
>>> 
http://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w
>>>
>>> http://flask.pocoo.org/docs/0.10/appcontext/
>>>
>>> However, it doesn't look like you are using a "create_app" method so
>>> db.init_app(app) seems pointless?  Either create the proper app function (
>>> http://flask.pocoo.org/snippets/20/) and select the proper app context
>>> for SQLalchemy as suggested in the above SO post or remove db.init_app(app)
>>> and replace it with db = SQLAlchemy(app).
>>>
>>> On Thu, Oct 23, 2014 at 9:29 AM, Blake Adams <blakesadams@gmail.com>
>>> wrote:
>>>
>>>> You can ignore the inputRequired - it was used ii another field that i
>>>> cutout for expediency.
>>>>
>>>> Full stacktrace below:
>>>>
>>>> Traceback (most recent call last):
>>>>
>>>>   File "manage.py", line 8, in <module>
>>>>
>>>>     from app import app, models, db
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/__init__.py", line 17, in <module>
>>>>
>>>>     from .main import main as main_blueprint
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/main/__init__.py", line 4, in
>>>> <module>
>>>>
>>>>     from . import routes, helper, forms
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/main/routes.py", line 5, in
>>>> <module>
>>>>
>>>>     from .forms import prod_entry, sku_list_search
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 7, in
>>>> <module>
>>>>
>>>>     class prod_entry(Form):
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/main/forms.py", line 11, in
>>>> prod_entry
>>>>
>>>>     category = SelectField('Category',
>>>> choices=get_categories(),coerce=int)
>>>>
>>>>   File "/Users/myfolder/Dev/myapp/app/main/helper.py", line 8, in
>>>> get_categories
>>>>
>>>>     for cat in db.session.query(Category).all():
>>>>
>>>>   File
>>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/orm/scoping.py",
>>>> line 150, in do
>>>>
>>>>     return getattr(self.registry(), name)(*args, **kwargs)
>>>>
>>>>   File
>>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/sqlalchemy/util/_collections.py",
>>>> line 878, in __call__
>>>>
>>>>     return self.registry.setdefault(key, self.createfunc())
>>>>
>>>>   File
>>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>>> line 704, in create_session
>>>>
>>>>     return SignallingSession(self, **options)
>>>>
>>>>   File
>>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>>> line 149, in __init__
>>>>
>>>>     self.app = db.get_app()
>>>>
>>>>   File
>>>> 
"/Users/myfolder/Dev/myapp/myappenv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py",
>>>> line 845, in get_app
>>>>
>>>>     raise RuntimeError('application not registered on db '
>>>>
>>>> RuntimeError: application not registered on db instance and no
>>>> application bound to current context
>>>>
>>>> On Thu, Oct 23, 2014 at 9:18 AM, Eric B <neurosnap@gmail.com> wrote:
>>>>
>>>>> In your imports i see: InputRequir ed
>>>>>
>>>>> Is that how it looks in the app?
>>>>>
>>>>> What's the error it is throwing you?
>>>>> On Oct 23, 2014 9:04 AM, "Blake Adams" <blakesadams@gmail.com> wrote:
>>>>>
>>>>>> My app is built around the 'main' blueprint.   I created a helper.py
>>>>>> within 'main' to use a function for my form.py file to dynamically generate
>>>>>> list from a model to be used in a SelectField.  I keep getting the 'app not
>>>>>> registered on db" error.
>>>>>>
>>>>>> Main parts of my file structure are:
>>>>>>
>>>>>>
>>>>>>    - myproject
>>>>>>       - app/
>>>>>>          - __init__.py
>>>>>>          - admin/
>>>>>>          - main/
>>>>>>             - __init__.py
>>>>>>             - forms.py
>>>>>>             - routes.py
>>>>>>             - helper.py
>>>>>>          - models.py
>>>>>>          - forms.py
>>>>>>          - static/
>>>>>>          - templates/
>>>>>>       - manage.py
>>>>>>       - config.py
>>>>>>
>>>>>> I originally had a hard coded ch oice list for a Select field in
>>>>>> forms.py - everything worked fine.  To make things dynamics I created
>>>>>> helper.py below:
>>>>>>
>>>>>> *# helper.py*
>>>>>> from . import main
>>>>>> from ..models import Model1, Model2
>>>>>> from app import db
>>>>>>
>>>>>> def get_categories():
>>>>>>     cat_list = []
>>>>>>     for cat in db.session.query(Model1).all():
>>>>>>     cat_list.append((cat.id,cat.category))
>>>>>>     return cat_list
>>>>>>
>>>>>> When i run the the 'get_categories' function in the shell works fine
>>>>>> but when i try to use it in forms.py to make dynamic list, app breaks.
>>>>>> Have done a lot of searching and tried several difference things (went from
>>>>>> app factory to instant app creation but didn't help).  What am I doing
>>>>>> wrong and is there a best practice for where to put helper functions?
>>>>>>
>>>>>>
>>>>>> Heres the forms.py file:
>>>>>> *# forms.py*
>>>>>> from flask.ext.wtf import Form
>>>>>> from wtforms import StringField, SelectField
>>>>>> from wtforms.fields.html5 import DateField
>>>>>> from wtforms.validators import Required, InputRequir ed, Length, Email
>>>>>> from .helper import get_categories
>>>>>>
>>>>>> class prod_entry(Form):
>>>>>>     ID = StringField('ID',validators= [Required(),Length(max=32)])
>>>>>>     name = StringField('name',validators= [Required(),Length(max=64)])
>>>>>>     brand = StringField('Brand',validators= [Length(max=64)])
>>>>>>     category = SelectField('Category',
>>>>>> choices=get_categories(),coerce=int)
>>>>>>
>>>>>> Also heres my app/__init__.py file:
>>>>>>
>>>>>> #__init__.py
>>>>>>
>>>>>> import os
>>>>>> from flask import Flask
>>>>>> from config import config
>>>>>> from flask.ext.sqlalchemy import SQLAlchemy
>>>>>>
>>>>>> db = SQLAlchemy()
>>>>>>
>>>>>> from flask.ext.bootstrap import Bootstrap
>>>>>>
>>>>>> bootstrap = Bootstrap()
>>>>>>
>>>>>> app = Flask(__name__)
>>>>>> app.config.from_object(config[os.geten v('FLASK_CONFIG') or
>>>>>> 'default'])
>>>>>> bootstrap.init_app(app)
>>>>>> db.init_app(app)
>>>>>>
>>>>>> from .main import main as main_blueprint
>>>>>> app.register_blueprint(main_blueprint)
>>>>>>
>>>>>>
>>>>>> Thanks Much,
>>>>>>
>>>>>> Blake
>>>>>>
>>>>>
>>>>
>>>
>>