Search code examples
pythonflaskjinja2wtformsflask-principal

Populate WTForm MultiCheckboxField with Flask-Principal Roles


I'm working on an edit user page for my flask app, and I can't seem to figure out how to render the user's current roles like I do other variables, such as email.

Here's my models, form, and view:

#Flask-Principal Role Model
class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

    def __repr__(self):
        return '<Role %r>' % (self.name)

#SQLALchemy User Model
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    first_name = db.Column(db.String(128))
    last_name = db.Column(db.String(128))
    business_name = db.Column(db.String(128))
    active = db.Column(db.Boolean())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

#WTForms User Form
class UserForm(Form):
    first_name = StringField('first name', validators= [Required()])
    last_name = StringField('last name', validators= [Required()])
    business_name = StringField('business name', validators= [Required()])
    email = StringField('email', validators = [Required(), Email()])
    active = BooleanField('active')
    roles = MultiCheckboxField('roles', coerce=int)

#Edit User View
@app.route('/admin/users/<id>/edit/', methods = ['GET', 'POST'])
@roles_required('admin')
def edit_user(id):
    user = User.query.filter_by(id = id).first()
    editform = UserForm()

# This is how I've assigned choices for other MultiCheckboxField forms, but I haven't 
# needed to populate the MultiCheckboxField from a user model before with role objects.

    editform.roles.choices = [(x.id,x.name) for x in Role.query.all()]

    if editform.validate_on_submit():
        pass

    editform.first_name.data = user.first_name
    editform.last_name.data = user.last_name
    editform.business_name.data = user.business_name
    editform.email.data = user.email
    editform.active.data = user.active

#The below doesn't show all the available roles, just the current roles assigned.

    editform.roles.data = user.roles


    return render_template("user_edit.html",
        title = "Edit User",
        user = user,
        editform = editform)

So then, does anyone know how to have WTForms display all the Roles available, and have checked the ones which are currently in the user.roles list?


Solution

  • data for a multi-select field is a list of form_data after it has been coerced by your coerce callable - so what you provide needs to match what's available as the first entry in each tuple you provide to choices - in this case, an integer. That said, if you change your code to:

    editform.roles.data = [role.id for role in user.roles]
    

    you should see all the appropriate checkboxes checked.