7

状态字段显示 3 个选项,但在提交时始终显示“不是有效选择”,无论选择哪个。

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))
4

1 回答 1

18

当您在 db 中的状态字段是整数时,选择很可能作为 unicode 字符串发送。您可以尝试以下方法:

form_args = dict(
    status=dict(
        choices=[(0, 'Regular'), (1, 'Guest'), (2, 'Banned')],coerce=int
    ))
于 2013-01-30T20:01:19.047 回答