我已经把这个问题从我的数据库大规模部署到我可以制作的最淡化的版本,但我仍然收到此错误。
下面是使用Flask_SQLAlchemy的非常简单的数据库结构的代码。
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////' + os.path.join(basedir, 'app.db')
app.config['SECRET_KEY'] = 'mysec'
db = SQLAlchemy(app)
class Client(db.Model):
id = db.Column(db.Integer, primary_key=True)
clientname = db.Column(db.String(50))
sessions = db.relationship('Session', backref='client', lazy='dynamic')
def __repr__(self):
return '<Client %r>' % (self.clientname)
class Session(db.Model):
id = db.Column(db.Integer, primary_key=True)
sessionname = db.Column(db.String(50))
accept = db.Column(db.String(50))
client_id = db.Column(db.Integer, db.ForeignKey('client.id'))
def __repr__(self):
return '<Session %r>' % (self.sessionname)
admin = Admin(app)
admin.add_view(ModelView(Client, db.session))
admin.add_view(ModelView(Session, db.session))
if __name__ == '__main__':
app.run(debug=True)
这很好用,数据库构建正常。这些关系似乎没问题,但是当我去Flask-Admin将记录添加到客户端或会话时(并且只有当)在另一个表中有记录时,我得到以下错误:
[2018-01-16 16:30:17,394] ERROR in app: Exception on /admin/session/new/ [GET]
Traceback (most recent call last):
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/model/base.py", line 2024, in create_view
return_url=return_url)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/base.py", line 308, in render
return render_template(template, **kwargs)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/model/create.html", line 3, in top-level template code
{% from 'admin/lib.html' import extra with context %} {# backward compatible #}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/master.html", line 1, in top-level template code
{% extends admin_base_template %}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/base.html", line 35, in top-level template code
{% block page_body %}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/base.html", line 64, in block "page_body"
{% block body %}{% endblock %}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/model/create.html", line 22, in block "body"
{% block create_form %}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/model/create.html", line 23, in block "create_form"
{{ lib.render_form(form, return_url, extra(), form_opts) }}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/lib.html", line 212, in template
{% call form_tag(action=action) %}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/lib.html", line 191, in template
{{ caller() }}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/lib.html", line 213, in template
{{ render_form_fields(form, form_opts=form_opts) }}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/lib.html", line 183, in template
{{ render_field(form, f, kwargs) }}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/templates/bootstrap2/admin/lib.html", line 137, in template
{{ field(**kwargs)|safe }}
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/wtforms/fields/core.py", line 153, in __call__
return self.meta.render_field(self, kwargs)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/wtforms/meta.py", line 56, in render_field
return field.widget(field, **render_kw)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/form/widgets.py", line 28, in __call__
return super(Select2Widget, self).__call__(field, **kwargs)
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/wtforms/widgets/core.py", line 287, in __call__
for val, label, selected in field.iter_choices():
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/contrib/sqla/fields.py", line 109, in iter_choices
for pk, obj in self._get_object_list():
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/contrib/sqla/fields.py", line 102, in _get_object_list
self._object_list = [(text_type(get_pk(obj)), obj) for obj in query]
File "/home/maingame/come_flask_obt/env/local/lib/python2.7/site-packages/flask_admin/contrib/sqla/fields.py", line 299, in get_pk_from_identity
cls, key = identity_key(instance=obj)
ValueError: too many values to unpack
答案 0 :(得分:2)
感谢@Doobeh,只需添加&#39; sqlalchemy == 1.2.0b3 &#39;到我的requirements.txt并安装它。较新版本的SQLAlchemy似乎不适用于Flask-Admin的某些关系类型。
答案 1 :(得分:2)
现在应该解决这个问题。根据{{3}} Flask-Admin,如果直接从主分支中提取sqlalchemy,它应该可以正常工作。