flask-wtforms。 QuerySelectField RuntimeError
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask-wtforms。 QuerySelectField RuntimeError相关的知识,希望对你有一定的参考价值。
我试图在FlaskForm中使用QuerySelectField
,但出现错误。
admin/forms.py
class ServiceForm(FlaskForm):
# def __init__(self, *args, **kwargs):
# super(ServiceForm, self).__init__(*args, **kwargs)
# self.category.choices = [(cat.id, cat.name) for cat in ServiceCategory.query.all()]
category = QuerySelectField('Category', query_factory=ServiceCategory.query,
get_pk=lambda a: a.id,
get_label=lambda a: a.name)
name = StringField('Name', validators=[DataRequired()])
description = TextAreaField('Description')
price = IntegerField('Price', validators=[DataRequired()])
duration = TimeField('Duration', validators=[DataRequired()])
submit = SubmitField('Submit')
def validate_name(self, name):
service = Service.query.filter_by(name=name.data).scalar()
if service:
raise ValidationError('Service already exist.')
错误:
127.0.0.1 - - [10/Jan/2020 23:02:27] "GET /admin/services HTTP/1.1" 500 -
Traceback (most recent call last):
File "/source/project/venv/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1010, in __call__
return self.registry[key]
KeyError: 123145443217408
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/source/project/venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/source/project/project.py", line 4, in <module>
app = create_app()
File "/source/project/app/__init__.py", line 24, in create_app
from app.admin import bp as admin_bp
File "/source/project/app/admin/__init__.py", line 5, in <module>
from app.admin import routes
File "/source/project/app/admin/routes.py", line 7, in <module>
from app.admin.forms import DateForm, ScheduleForm, ServiceCategoryForm, ServiceForm
File "/source/project/app/admin/forms.py", line 75, in <module>
class ServiceForm(FlaskForm):
File "/source/project/app/admin/forms.py", line 80, in ServiceForm
category = QuerySelectField('Category', query_factory=ServiceCategory.query,
File "/source/project/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 519, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/source/project/venv/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
return self.registry()
File "/source/project/venv/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1012, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/source/project/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 3234, in __call__
return self.class_(**local_kw)
File "/source/project/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 136, in __init__
self.app = app = db.get_app()
File "/source/project/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 982, in get_app
'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
答案
或def category_choice():
return ServiceCategory.query
class ServiceForm(FlaskForm):
category = QuerySelectField('Category', query_factory=category_choice, get_label='name')
...
以上是关于flask-wtforms。 QuerySelectField RuntimeError的主要内容,如果未能解决你的问题,请参考以下文章
flask-wtforms。 QuerySelectField RuntimeError