How to make a field not editable in Flask Admin view for model class

I have a User model class, and password is one of many attributes. I use the flash framework and the Flask-Admin extension to create an admin view for my model classes. I want certain fields in the admin view like password not to be edited or not to display at all. How to do it?

I can make the fields not visible in normal mode, but when I click the edit button for any record in the table, all fields are displayed and edited.

+7
python flask flask-admin
source share
3 answers

You must expand your view from ModelView and overwrite the required 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:

+14
source share

Here is a solution that expands after Remo's answer, and this one so answer . It allows you to use various fields for editing and creating forms.

Custom Field Rule Class

 from flask_admin.form.rules import Field class CustomizableField(Field): def __init__(self, field_name, render_field='lib.render_field', field_args={}): super(CustomizableField, self).__init__(field_name, render_field) self.extra_field_args = field_args def __call__(self, form, form_opts=None, field_args={}): field_args.update(self.extra_field_args) return super(CustomizableField, self).__call__(form, form_opts, field_args) 

Class userview

 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_edit_rules = [ CustomizableField('name', field_args={ 'readonly': True }), # ... place other rules here ] 
+6
source share

Another way to solve this problem is to use the Flask-Admin ModelView modeling method called on_form_prefill to set the readonly property argument. According to Flask-Admin Docs :

on_form_prefill (form, identifier)

Follow these additional steps to pre-populate the edit form.

Called from edit_view if the current action is rendering the form rather than receiving input on the client side after the default prefilling has been performed.

In other words, this is a trigger that starts when you open only the edit form, not Create.

So, the solution for the example used above would be:

 class UserView(ModelView): ... def on_form_prefill(self, form, id): form.name.render_kw = {'readonly': True} 

The method starts after applying all the other rules, so none of them are broken, including a set of columns.

+1
source share

All Articles