ajay ajay - 4 months ago 40
Python Question

How to make a field non-editable in Flask Admin view of a model class

I have a

User
model class and
password
is one attribute among many. I am using Flask web framework and Flask-Admin extension to create the admin view of my model classes. I want to make certain fields in the admin view like
password
non editable or not show them at all. How do I do it?

I can make the fields not show up in the normal view but when I click on the edit button of any record in the table, all fields show up and are editable.

Answer

You should extend your view from ModelView and overwrite the necessary fields.

In my class it looks like this:

class UserView(ModelView):

    column_list = ('first_name', 'last_name', 'username', 'email')
    searchable_columns = ('username', 'email')
# this is to exclude the password field from list_view:
    excluded_list_columns = ['password']
    can_create = True
    can_delete = False
# If you want to make them not editable in form view: use this piece:
    form_widget_args = {
        'name': {
            'readonly': True
        },
    }

Hope this helps! For more information check out the documentation: