简体   繁体   English

基于db的Flask-SQLAlchemy wtform

[英]Flask-SQLAlchemy wtform based on db

My app is working on my main PC. 我的应用程序正在主PC上运行。 I tried to clone the app to my laptop (I tried to initialize it on PC in a different directory and the problem was the same so the problem isn't with the laptop) and when initializing the db with flask db init I got the following error: 我试图将应用程序克隆到笔记本电脑上(我试图在PC上的其他目录中对其进行初始化,并且问题是相同的,所以问题不在于笔记本电脑),并且在使用flask db init初始化db时,我得到了以下内容错误:

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)

What I'm trying to do is in one of my forms to include a wtforms RadioField with choices based on the race table in db. 我想做的是在一种形式中包括wtforms RadioField以及基于db中种族表的选择。 So for every row in that table I want one choice in the selection. 因此,对于该表中的每一行,我都希望选择一个选项。 The important part of the form looks like this: 表单的重要部分如下所示:

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

And the races variable is outside the form like this (before the form itself): races变量在这样的形式之外(在形式本身之前):

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

I figured that this last line is the problem from the call stack: 我发现这最后一行是调用堆栈中的问题:

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)

So the problem is that I don't have the db initialized when I'm running the flask db init command and that one line needs the db to exist. 所以问题是,当我运行flask db init命令时,我没有初始化flask db init ,并且一行需要数据库存在。 This probably doesn't have a solution so I'm not asking how to correct this implementation, but how to do it correctly to have form based on rows from the db. 这可能没有解决方案,所以我不是在问如何更正此实现,而是要如何正确地实现基于数据库行的表单。 I don't have hundreds of races so I could hardcode them in but I wanted to do it this way. 我没有几百场比赛,所以我可以对它们进行硬编码,但是我想这样做。

So after some googling I found that you can add the RadioField choices dynamically in the views like this: 因此,经过一番谷歌搜索后,我发现您可以在如下视图中动态添加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()]

This is exactly what I was looking for. 这正是我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM