为什么这个烧瓶管理员总是没有通过验证?

时间:2013-01-30 18:42:24

标签: python sqlalchemy flask wtforms

状态字段显示3个选项,但在submisison上始终显示“Not a valid choice”,无论选择哪个。

from website import app, db
from flask.ext import admin
from flask.ext.admin.contrib import sqlamodel
from wtforms.fields import SelectField


class Users(db.Model):
    id     = db.Column(db.Integer, primary_key=True)
    name   = db.Column(db.String(160), unique=True)
    status = db.Column(db.Integer)

    def __repr__(self):
        return self.name


class UsersAdmin(sqlamodel.ModelView):
    form_overrides = dict(status=SelectField)
    form_args = dict(
        status=dict(
            choices=[(0, 'Regular'), (1, 'Guest'), (2, 'Banned')],
        ))

admin = admin.Admin(app, 'Admin')
admin.add_view(UsersAdmin(Users, db.session))

1 个答案:

答案 0 :(得分:16)

当db中的status字段为整数时,可能会将选项作为unicode字符串发送。你能尝试以下方法:

form_args = dict(
    status=dict(
        choices=[(0, 'Regular'), (1, 'Guest'), (2, 'Banned')],coerce=int
    ))