Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this flask-admin form alway failing validation?

The status field shows the 3 options, but always displays "Not a valid choice" on submisison, whichever is chosen.

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))
like image 297
user713713 Avatar asked Jan 30 '13 18:01

user713713


1 Answers

It is likely that the choices are being sent as unicode string while your status field in db is integer. Can you try the following:

form_args = dict(
    status=dict(
        choices=[(0, 'Regular'), (1, 'Guest'), (2, 'Banned')],coerce=int
    ))
like image 77
codegeek Avatar answered Sep 19 '22 07:09

codegeek