基于db的Flask-SQLAlchemy wtform

时间:2018-07-29 09:11:09

标签: python flask sqlalchemy flask-sqlalchemy

我的应用程序正在主PC上运行。我试图将应用程序克隆到笔记本电脑上(我试图在PC上的其他目录中对其进行初始化,并且问题是相同的,所以问题不在于笔记本电脑),并且在使用flask db init初始化数据库时,我得到了以下错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: race [SQL: 'SELECT race.id AS race_id, race.name AS race_name \nFROM race'] (Background on this error at: http://sqlalche.me/e/e3q8)

我想做的是在一种形式中包括wtforms RadioField,并根据db中的 race 表进行选择。因此,对于该表中的每一行,我都希望选择一个选项。表单的重要部分如下所示:

class RegistrationForm(FlaskForm):
    race = RadioField('Race', validators=[InputRequired('Choose a race.')],
                      choices=races, coerce=int)

种族变量在这样的表单外部(在表单本身之前):

with create_app().app_context():
    races = [(race.id, race.name.capitalize()) for race in Race.query.all()]

我认为这最后一行是调用堆栈中的问题:

Traceback (most recent call last):
  File "/home/dareon4/workspace/test/venv/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/flask/cli.py", line 411, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/flask/cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/home/dareon4/workspace/test/shapes.py", line 5, in <module>
    app = create_app()
  File "/home/dareon4/workspace/test/app/__init__.py", line 43, in create_app
    from app.blueprints.auth import bp as auth_bp
  File "/home/dareon4/workspace/test/app/blueprints/auth/__init__.py", line 7, in <module>
    from . import routes
  File "/home/dareon4/workspace/test/app/blueprints/auth/routes.py", line 13, in <module>
    from .forms import LoginForm, RegistrationForm, ResetPasswordRequestForm, \
  File "/home/dareon4/workspace/test/app/blueprints/auth/forms.py", line 19, in <module>
    races = [(race.id, race.name.capitalize()) for race in Race.query.all()]
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/orm/query.py", line 2836, in all
    return list(self)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/orm/query.py", line 2988, in __iter__
    return self._execute_and_instances(context)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/orm/query.py", line 3011, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/home/dareon4/workspace/test/venv/lib64/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: race [SQL: 'SELECT race.id AS race_id, race.name AS race_name \nFROM race'] (Background on this error at: http://sqlalche.me/e/e3q8)

所以问题是我在运行flask db init命令时没有初始化数据库,并且一行需要数据库存在。这可能没有解决方案,所以我不是在问如何更正此实现,而是要如何正确地实现基于数据库行的表单。我没有几百场比赛,所以我可以对它们进行硬编码,但是我想这样做。

1 个答案:

答案 0 :(得分:3)

因此,经过一番谷歌搜索后,我发现您可以在这样的视图中动态添加RadioField选项:

@bp.route('/register', methods=['GET', 'POST'])
def register():
    form = RegistrationForm()
    form.race.choices = [(race.id, race.name.capitalize()) for race in Race.query.all()]

这正是我想要的。