简体   繁体   中英

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: 在此处输入图片说明 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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM