0

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..?

Tri
  • 2,722
  • 5
  • 36
  • 65
  • 1
    have you tried this solution already? https://stackoverflow.com/questions/17137601/flask-admin-can-create-true-only-for-a-given-user – gittert Apr 02 '19 at 09:55

1 Answers1

0

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.

Tri
  • 2,722
  • 5
  • 36
  • 65
  • 1
    If you are looking for different admin views per role, a solution could be to add multiple flask-admin instances like this https://github.com/flask-admin/flask-admin/issues/55 – gittert Apr 02 '19 at 10:40
  • That's not seems I can use `sqla.ModelView` there, what I want is to manage two models in different `ModelView` base on user roles. – Tri Apr 03 '19 at 09:45
  • 1
    Add the view twice (use different names to avoid blueprint collisions). Set up each one for a different role access and setup each view with the necessary settings. That should do the trick. – gittert Apr 03 '19 at 15:14