Search code examples
pythonflaskflask-sqlalchemyflask-adminflask-security

How to separate access in methods from BaseModelView class in Flask-Admin


I'm using Flask-Admin to manage my dashboard, there are few roles in my app, two of them are superuser and client.

Now, I have User table build by Flask-SQLAlchemy, and now I want to manage the table by users role. Which means the superuser can edit user roles in the Flask-Admin form, and in the other client can't do it or the form are not displayed.

Like this screenshot: enter image description here there are edit form by superuser role. But for now, I want the edit form not displayed if the current user has role client.

So, in logically I want to making something like this:

class UserModelView(sqla.ModelView):
    if current_user.has_role('superuser'):
        form_excluded_columns = ('created_at', 'updated_at')
    else:
        form_excluded_columns = ('created_at', 'updated_at', 'roles')

Solution

  • I following @SergeyShubin advice to following his answer here and in that case are very similar with my case and it works perfectly.

    Also @SergeyShubin answer here also similar with my case. Thanks very much @SergeyShubin.