Flask admin overrides password when user model is changed
Might be easier to override the get_edit_form
method and delete the password field entirely from the edit form.
class UserView(MyModelView):
def get_edit_form(self):
form_class = super(UserView, self).get_edit_form()
del form_class.password
return form_class
Another alternative would be to remove the model password field entirely from the form and use a dummy password field that can then be used to populate the model's password. By removing the real password field Flask-Admin will not step on our password data. Example :
class UserView(MyModelView):
form_excluded_columns = ('password')
# Form will now use all the other fields in the model
# Add our own password form field - call it password2
form_extra_fields = {
'password2': PasswordField('Password')
}
# set the form fields to use
form_columns = (
'username',
'email',
'first_name',
'last_name',
'password2',
'created_at',
'active',
'is_admin',
)
def on_model_change(self, form, User, is_created):
if form.password2.data is not None:
User.set_password(form.password2.data)