Search code examples
pythonflaskflask-sqlalchemyflask-admin

How to separate access Flask-Admin on can_create?


I want to making a condional on Flask-Admin methods feature.

But confusing about how to give a conditional between users roles, let says in the can_create feature.

Here is the snippet of my modelview:

class UserModelView(sqla.ModelView):

    if current_user.has_role == 'superuser':
        can_create = True
    elif current_user.has_role == 'client':
        can_create = False

But I'm getting few errors, and I have also tried the different ways, like this:

class UserModelView(sqla.ModelView):
    def is_visible(self):
        if current_user.has_role == 'superuser':
            can_create = True
        elif current_user.has_role == 'client':
            can_create = False

and I have also tried it with other methods on BaseModelView class, but still don't work like I want.

So.. is it possible to give conditional in that feature..?


Solution

  • I following this solution base on @gitter advice, and it works in can_create feature, thanks @gitter.

    But unfortunately it won't work in another conditional like form_excluded_columns

    .

    EDIT: For methods (ie: can_create) from BaseModelView Class I following this solution

    But for another methods like form_excluded_columns, I'm using this way , and this way can also work.