Search code examples
pythonflaskflask-admin

Pre-filter readable data based on user permissions with Flask-Admin


I'm using flask-admin to create a simple backend for my database. I'd like to only show parts of a table, based on a pre-filtering, based on the permissions the user has. For example, if the table has all the sales of a company, i would like to only show the sales of salesperson 'foo' when 'foo' accesses the backend


Solution

  • I found the solution is overriding the method 'get_query'

    It should return a SQLAlchemy query object.

    def get_query(self):
        role = current_user.role
        if role == 'contributor':
            return # filtered query
        elif role == 'admin':
            return # unfiltered query