user3666149
user3666149

Reputation: 133

Flask-Admin Modelview configurations

I'm trying to tweak the Flask-Admin Modelview to only show certain columns in the Edit and Create tabs. So far I've been able to configure the listing correctly by putting this in my subclass:

class AuthUserView(AuthModelView):
    column_list = ('username', 'email', 'roles', 'last_login_at', 'login_count')

Works great! But now my issues are this. There's also a password field which is attached to a User. When the edit tab is open, I'd prefer that the column was left out of the edit field. Furthermore, when I create a user, I'd like the password field to be there, but to encrypt the input before storing it in the database (Meaning I just need to intercept the data between "submit" and actually storing it".

Is it possible to make these customizations? Thanks for any help!

Edit: So I was able to get the password field removed by adding this code to my view:

form_excluded_columns = ('password')

Except now this removes it from BOTH the edit and the create forms. I only want it removed from one though.

Upvotes: 2

Views: 1821

Answers (1)

user3666149
user3666149

Reputation: 133

Got it working! Figure I'll put my answer here if anybody else comes across the issue... Here's what my final AuthUserView looked like

class AuthUserView(AuthModelView):
    form_edit_rules = ('roles', 'active', 'username', 'email', 'confirmed_at', 'last_login_at', 'current_login_at', 'last_login_ip', 'current_login_ip', 'login_count')
    column_list = ('username', 'email', 'roles', 'last_login_at', 'login_count')

    def create_model(self, form):
        form.password.data = encrypt_password(form.password.data)
        super(AuthUserView, self).create_model(form)

I used the form_edit_rules to remove the password key from the edit field. Then I override create_model to encrypt the password, and then call the super method to actually create the model.

Upvotes: 6

Related Questions