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

前端 未结 3 779
离开以前
离开以前 2021-02-20 13:03

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 o

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 13:43

    Yet another way to work the problem around is to use Flask-Admin ModelView method called on_form_prefill to set readonly property argument. According to Flask-Admin Docs:

    on_form_prefill(form, id)

    Perform additional actions to pre-fill the edit form.

    Called from edit_view, if the current action is rendering the form rather than receiving client side input, after default pre-filling has been performed.

    In other words, this is a trigger, which is run when opening only Edit form, not the Create one.

    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 is run after all other rules applied, so none of them are broken, including set of columns.

提交回复
热议问题