Search code examples
pythonflaskflask-sqlalchemyflask-admin

flask-admin: how to customize the view according to the individual user instead of the role?


As we all know, flask-admin allows the users with same role have the same privilege. However, in my app I'd like to restrict only the user who created a query to view and edit.

For example, there are many users with the same role called "developers", he/she creates a code review and wait for approval.

The pic below shows developer Gina can see developer Bill's project in the list(which is not desired). What I'd like to achieve is only Gina and the reviewers(Wesker and Steve here) can view this project in the list.

I've put the code here , if it's not appropriate I will remove the link. Thanks in advance.

enter image description here


Solution

  • Overriding ModelView, you can filter results to show logged user own results:

    def get_query(self):
      return self.session.query(self.model).filter(self.model.user==current_user)
    
    def get_count_query(self):
      return self.session.query(func.count('*')).filter(self.model.user==current_user)
    

    You can find more info here: Flask-Admin default filters