我正在尝试使用QuerySelectField。但是,我不断收到此错误:
“要解包的值太多(期望两个)”
我不知道如何解决这个问题。没有线索或任何方向。 我将尝试更好地详细说明此问题,但我一点也不了解。其他Stack Overflow用户也提出了类似的问题。但是,据我所知,这个错误是由Wtf格式的弃用引起的(至少从我的理解)。
Why does Flask WTForms and WTForms-SQLAlchemy QuerySelectField produce too many values to unpack?
这很可能是导致错误的原因,但我不认为这是导致错误的原因。原因是要做到这一点,我实际上是在学习教程。
https://www.youtube.com/watch?v=b9W2ul2VRRc
本教程的数据库模型与我的非常相似。我几乎完全按照他的指示进行了一些小的更改,因为我正在处理的项目不仅包含在一个单一的文件中。据我所知,这些更改如下:
1:我要查询创建的表的名称,而不是他创建的表的名称。
2:我有一个模型,路由,Init等python文件,因此他创建的类和函数在稍微不同的位置。
更改我认为不是问题或根本原因。
我将非常感谢任何建议或帮助,任何人都可以给我。即使您不能或选择不提供帮助,也请您仔细阅读本文。
以下是相关代码:
from datetime import datetime
from app import db, login
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
class GuildModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
GuildName = db.Column(db.String(300), index=True, unique=True)
GuildDescription = db.Column(db.String(64))
def __repr__(self):
return '<Guild {}>'.format(self.GuildName)
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from app.models import UserModel, GuildModel
from wtforms_sqlalchemy.fields import QuerySelectField
class GuildRegistrationForm(FlaskForm):
GuildName = StringField('Please enter the name of your guild', validators=[DataRequired()])
GuildDescriptionForm = StringField('Describe your guild!', validators=[DataRequired()])
SubmitGuild = SubmitField('Create Guild!')
def GuildQuery():
return GuildModel.query
class GuildSetForm(FlaskForm):
GuildOneName = QuerySelectField(query_factory=GuildQuery, allow_blank=True)
from app import app
from app import db
from flask import render_template, flash, redirect, url_for, request
from app.forms import UserLoginForm, UserRegistrationForm, GuildRegistrationForm, GuildSetForm
from flask_login import current_user, login_user, logout_user
from app.models import UserModel, GuildModel
@app.route('/SetGuilds')
def SetGuilds():
form = GuildSetForm()
return render_template('GuildSelect.html', form=form)
<form action="" method="POST">
<p>
{{ form.GuildOneName.label }}
{{ form.GuildOneName(size=2) }}
</p>
</form>
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/z/Desktop/DevBuild/app/routes.py", line 66, in SetGuilds
return render_template('GuildSelect.html', form=form)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/z/Desktop/DevBuild/app/templates/GuildSelect.html", line 4, in top-level template code
{{ form.GuildOneName(size=2) }}
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms/fields/core.py", line 155, in __call__
return self.meta.render_field(self, kwargs)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms/meta.py", line 56, in render_field
return field.widget(field, **render_kw)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms/widgets/core.py", line 323, in __call__
for val, label, selected in field.iter_choices():
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms_sqlalchemy/fields.py", line 107, in iter_choices
for pk, obj in self._get_object_list():
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms_sqlalchemy/fields.py", line 100, in _get_object_list
self._object_list = list((text_type(get_pk(obj)), obj) for obj in query)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms_sqlalchemy/fields.py", line 100, in <genexpr>
self._object_list = list((text_type(get_pk(obj)), obj) for obj in query)
File "/home/z/Desktop/GuildsSocialBuilds/DevBuild/venv/lib/python3.5/site-packages/wtforms_sqlalchemy/fields.py", line 189, in get_pk_from_identity
cls, key = identity_key(instance=obj)
ValueError: too many values to unpack (expected 2)