从SQLAlchemy DB填充FlaskForm,返回错误:

时间:2017-06-19 14:28:07

标签: python flask sqlalchemy

我正在调试错误,当我尝试从我的数据库查询以填充我的FlaskForm的字段时,我收到以下错误:

Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    app = create_app(os.getenv('DVR_CONFIG') or 'default')
  File "C:\Users\---\myapp\\app\__init__.py", line 42, in create_app
    from .main import main as main_blueprint
  File "C:\Users\---\myapp\\app\main\__init__.py", line 5, in <module>
    from . import views, errors
  File "C:\Users\---\myapp\\app\main\views.py", line 5, in <module>
    from .forms import CharacterSelect
  File "C:\Users\---\myapp\\app\main\forms.py", line 15, in <module>
    class CharacterSelect(FlaskForm):
  File "C:\Users\---\myapp\\app\main\forms.py", line 17, in CharacterSelect
    user = User.query.filter_by(unique_name=session.get('unique_name')).first()
  File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 500, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__
    return self.registry()
  File "C:\Python27\lib\site-packages\sqlalchemy\util\_collections.py", line 990, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 771, in create_session
    return SignallingSession(self, **options)
  File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 153, in __init__
    self.app = app = db.get_app()
  File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 922, in get_app
    raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context

以下是有问题的代码:

from ..models import User

class CharacterSelect(FlaskForm):
    user = User.query.filter_by(unique_name=session.get('unique_name')).first()
    charId0 = user.characterId_0
    charId1 = user.characterId_1
    charId2 = user.characterId_2
    user_list = [charId0, 'Char0', charId1, 'Char1', charId2, 'Char2']
    new_user_list = [x for x in user_list if x is not None]
    selectedUser = SelectField(u'Character Select', choices=user_list)

通过研究此错误,Flask似乎不知道我的数据库附加到哪个应用程序。此外,我见过推荐使用的大多数stackoverflow答案:

with app.app_context():
    :
    :
    db.create_all()

然而,通过Miguels Flask Book和Flasky博客,他似乎不需要使用&#34; app.app_context()&#34;并且只使用&#34; db.create_all()&#34;在他的单元测试代码中。 Miguel像这样进入DB:

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    :
    :
db.init_app(app)

非常感谢任何有关调试和理解此错误的帮助!

2 个答案:

答案 0 :(得分:0)

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.init_app(app)

    return app

您的代码段中有许多内容尚未明确。但显而易见的是,您必须在db.init_app(app)函数中使用create_app()。然后你将从函数返回app个对象。最好是在不同的模块中,即manage.py(这实际上取决于您的应用程序结构)

from somewhere import create_app

app = create_app(config_name)

if __name__ == "__main__":
    app.run()

现在,变量app是您注册了db的应用程序实例。

答案 1 :(得分:0)

有人指向我FlaskForm Docs,这是一个非常稀疏的例子。 基本上我需要在views.py中创建元组列表并将其传递给我的表单对象,详见下文:

views.py:

    user = g.user
    charId0 = user.characterId_0
    charId1 = user.characterId_1
    charId2 = user.characterId_2
    user_list = [(charId0, 'Char0'), (charId1, 'Char1'), (charId2, 'Char2')]

    form = CharacterSelect(request.form)
    form.selectedUser.choices = user_list

forms.py:

class CharacterSelect(FlaskForm):
    selectedUser = SelectField(u'Character Select', choices=[])