我正在使用WTForms' model_form
基于Flask-SQLAlchemy模型创建表单。如果模型上有外键,我会得到AttributeError: 'SQLAlchemy' object has no attribute 'query'
。为什么我会收到此错误以及如何解决?
from wtforms.ext.sqlalchemy.orm import model_form
class BREADView(MethodView):
def __init__(self,endpoint_param,model_param,exclude=None):
self.endpoint = endpoint_param;
self.model = model_param;
self.path = url_for('.%s' % self.endpoint)
self.DetailForm = model_form(self.model, db_session=db, base_class=Form )
self.DetailForm.submit = SubmitField('Submit')
def get(self, obj_id='', operation=''):
if operation == 'new':
new_form = self.DetailForm(request.form)
return render_template(
self.detail_template,
form=new_form,
)
Traceback (most recent call last):
File "/bin/user_wsgi_wrapper.py", line 130, in __call__
self.error_log_file.logger.exception("Error running WSGI application")
File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
self.error(msg, *args, **kwargs)
File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
self.handle(record)
File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
if (not self.disabled) and self.filter(record):
File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
for f in self.filters:
File "/bin/user_wsgi_wrapper.py", line 122, in __call__
app_iterator = self.app(environ, start_response)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.4/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/flask/views.py", line 149, in dispatch_request
return meth(*args, **kwargs)
File "/home/severedblue/mysite/app/uct/views.py", line 53, in get
endpoint=self.endpoint,
File "/usr/local/lib/python3.4/dist-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/usr/local/lib/python3.4/dist-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/jinja2/_compat.py", line 36, in reraise
raise value.with_traceback(tb)
File "/home/severedblue/mysite/app/templates/form.html", line 2, in <module>
{% import "bootstrap/wtf.html" as wtf %}
File "/home/severedblue/mysite/app/templates/base.html", line 1, in <module>
{% extends "bootstrap/base.html" %}
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in <module>
{% block doc -%}
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in <module>
{%- block html %}
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in <module>
{% block body -%}
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 23, in <module>
{% block content -%}
File "/home/severedblue/mysite/app/templates/base.html", line 23, in <module>
{% block page_content %}{% endblock %}
File "/home/severedblue/mysite/app/templates/form.html", line 11, in <module>
{{ wtf.quick_form(form) }}
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 195, in <module>
{{ form_field(field,
File "/home/severedblue/.local/lib/python3.4/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 116, in <module>
{{field(class="form-control", **kwargs)|safe}}
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/fields/core.py", line 149, in __call__
return self.meta.render_field(self, kwargs)
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/meta.py", line 53, in render_field
return field.widget(field, **render_kw)
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/widgets/core.py", line 280, in __call__
for val, label, selected in field.iter_choices():
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/ext/sqlalchemy/fields.py", line 107, in iter_choices
for pk, obj in self._get_object_list():
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/ext/sqlalchemy/fields.py", line 98, in _get_object_list
query = self.query or self.query_factory()
File "/home/severedblue/.local/lib/python3.4/site-packages/wtforms/ext/sqlalchemy/orm.py", line 121, in <lambda>
'query_factory': lambda: db_session.query(foreign_model).all()
AttributeError: 'SQLAlchemy' object has no attribute 'query'
答案 0 :(得分:0)
您已将model_form
指向SQLAlchemy扩展对象,而不是它提供的会话。请改用db.session
。
self.DetailForm = model_form(self.model, db_session=db.session, base_class=Form)