How to check if a user has a particular group

In Odoo, there is a has_group method in the model res.users. You can use this method in order to check if a user has a particular group. For example, if you want to check if the user is a salesman in the system, the code should look like:

user = self.env['res.users'].browse(self.env.uid)

if user.has_group('base.group_sale_salesman'):
    print 'This user is a salesman'
    print 'This user is not a salesman'

I found the hard way that using self.user for checking a group of a user, doesn’t work as it should. Therefore, I always suggest using browse to get the current user.


Leave a Reply

Your email address will not be published. Required fields are marked *