Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly text field in Flask-Admin ModelView

How can I make a field on a ModelView readonly?

class MyModelView(BaseModelView):
    column_list = ('name', 'last_name', 'email')
like image 440
user2071987 Avatar asked Feb 14 '13 12:02

user2071987


3 Answers

If you're talking about Flask-Admin with SQLAlchemy Models, and you're declaring a view by inheriting from sqlamodel.ModelView, you can just add this to your class definition:

class MyModelView(BaseModelView):
    column_list = ('name', 'last_name', 'email')
    form_widget_args = {
        'email':{
            'disabled':True
        }
    }
like image 106
Richard Aplin Avatar answered Oct 31 '22 03:10

Richard Aplin


I don't have enough reputation to comment on @thkang's answer, which is very close to what worked for me. The disabled attribute excludes the field from the POST data, but using readonly had the desired effect.

from wtforms.fields import TextField

class ReadonlyTextField(TextField):
  def __call__(self, *args, **kwargs):
    kwargs.setdefault('readonly', True)
    return super(ReadonlyTextField, self).__call__(*args, **kwargs)
like image 12
brab Avatar answered Oct 31 '22 03:10

brab


I got weird errors when I tried to use disabled for text fields, so I used readonly instead:

class MyModelView(BaseModelView):
    column_list = ('name', 'last_name', 'email')
    form_widget_args = {
        'email':{
            'readonly':True
        }
    }
like image 8
MF DOOM Avatar answered Oct 31 '22 03:10

MF DOOM