问题描述
我有一个非常基本的Flask应用程序:
I've got a pretty basic Flask app:
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms_sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from wtforms import StringField
db = SQLAlchemy()
app = Flask(__name__)
app.config['SQLALCHEMY_URI'] = 'sqlite:///:memory:'
app.config['SECRET_KEY'] = 'fnord'
db.init_app(app)
class Section(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.Text, nullable = False)
subject_id = db.Column(db.Integer, db.ForeignKey('subject.id'))
subject = db.relationship('Subject', back_populates='sections')
class Subject(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.Text, nullable = False)
sections = db.relationship('Section', back_populates='subject')
def do_something():
return Subject.query
class SelectionForm(FlaskForm):
name = StringField('Section Name', validators=[DataRequired()])
subject = QuerySelectField('Subject', query_factory=do_something, allow_blank=False, get_label='name')
with app.app_context():
db.create_all()
db.session.add(Subject(id=1, name='Science'))
db.session.add(Subject(id=2, name='Math'))
db.session.commit()
@app.route('/')
def main():
form = SelectionForm()
return render_template('index.html', form=SelectionForm())
app.run('0.0.0.0', port=5000, debug=True)
使用非常简单的模板:
<!DOCTYPE html>
<html>
<body>
{{ form.csrf_token }}
{{ form.name }}
{{ form.subject }}
</body>
</html>
与此有关的问题是我得到了ValueError: too many values to unpack (expected 2)
.这显然不是我所期望的-就我所知,我正在遵循我在网上看到的示例,但是这里有些不同.问题在于,无论wtforms_sqlalchemy/fields.py文件中发生了什么,它都得到了(<class '__main__.Subject'>, (1), None)
,而不是我原本期望的东西,因为它试图分配给cls, key
(<class '__main__.Subject'>, 1)
.
The problem with this is that I get ValueError: too many values to unpack (expected 2)
. This is obviously not what I expected - as far as I can tell I'm following the examples that I've seen online, but something is different here. The problem is that whatever is going on under the hood in the wtforms_sqlalchemy/fields.py file, it's getting (<class '__main__.Subject'>, (1), None)
, instead of what I would have expected to probably be something else, since it's trying to assign to cls, key
, probably (<class '__main__.Subject'>, 1)
.
那我在做什么错了?
推荐答案
基于pjcunningham链接的补丁,由于wtforms尚未发布该更新,因此我继续创建自己的Monkeypatch:
Based on the patch that pjcunningham linked to, since wtforms hasn't released that update yet, I went ahead and created my own monkeypatch:
import wtforms_sqlalchemy.fields as f
def get_pk_from_identity(obj):
cls, key = f.identity_key(instance=obj)[:2]
return ':'.join(f.text_type(x) for x in key)
f.get_pk_from_identity = get_pk_from_identity
在我的代码库中放置该代码现在就可以正常工作,直到他们削减了与最新版本的SQLAlchemy一起使用的发行版为止.
Plopping that in my codebase is enough to work for now until they cut a release that works with the newest version of SQLAlchemy.
这篇关于为什么Flask WTForms和WTForms-SQLAlchemy QuerySelectField产生太多无法解包的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!