为什么 Flask WTForms 和 WTForms-SQLAlchemy QuerySelectField 会产生太多的值来解压? [英] Why does Flask WTForms and WTForms-SQLAlchemy QuerySelectField produce too many values to unpack?

查看:25
本文介绍了为什么 Flask WTForms 和 WTForms-SQLAlchemy QuerySelectField 会产生太多的值来解压?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的 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).

那么我在这里做错了什么?

So what am I doing wrong here?

推荐答案

基于 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 会产生太多的值来解压?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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