flask-wtforms. QuerySelectField RuntimeError [英] flask-wtforms. QuerySelectField RuntimeError

查看:79
本文介绍了flask-wtforms. QuerySelectField RuntimeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在FlaskForm中使用QuerySelectField,但出现错误.

Im trying to use QuerySelectField in FlaskForm, but getting error.

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/.

推荐答案

 category = QuerySelectField('Category', query_factory=lambda: ServiceCategory.query, get_label='name')

def category_choice():
    return ServiceCategory.query


class ServiceForm(FlaskForm):
    category = QuerySelectField('Category', query_factory=category_choice, get_label='name')
    ...

这篇关于flask-wtforms. QuerySelectField RuntimeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆