WTForms obj = sqlalchemy.Model不工作?

时间:2016-01-04 17:49:06

标签: python flask sqlalchemy wtforms

我正在使用Flask,SQLAlchemy和WTForms。这是我的片段:

class AccountForm(Form):
    name     = StringField(validators=[validators.Optional(),], filters = [lambda x: x or None])
    email    = StringField(validators=[validators.Optional(), validators.Email()], filters = [lambda x: x or None])
    password = PasswordField(validators=[validators.Optional()], filters = [lambda x: x or None])


class Account(Model):
    __tablename__   = 'accounts'

    id              = db.Column(db.Integer, primary_key=True)
    name            = db.Column(db.String(250), nullable=False)
    email           = db.Column(db.String(250), nullable=False, unique=True)
    password        = db.Column(db.String(250), nullable=True, default=None)

然后我只使用/account/参数向name发出PUT请求:

@app.route("/account/", methods=['PUT',])
@authenticated
def update():
    # g.account contains the instance of the Model Account
    form = AccountForm(request.form, obj=g.account)


    # form.email.data is empty !
    # It should be set at the value from the Account model ?!

    if not form.validate():
        return form.errors_as_json()

    form.email.data is None # True

我按照文档,尝试了几个替代方案(populate_obj等),没有任何运气,我无法从表单中加载模型中的数据!

我错过了什么?

提前谢谢。

0 个答案:

没有答案