Search code examples
pythonodoo

Check if a user belongs to a group in Odoo


I'm new to Odoo, i have a selection field and one of the options should be reserved only for a certain groupe, the field's name is event_type and the group's name is TL, i tried this but it doesn't work properly.

@api.multi
@api.onchange('event_type')
def _test(self):
    if self.event_type == 'HV':
        if self.env.user.has_group('TL')==False:
            raise ValidationError('Only user belongs to TL are allowed to ....' )

Solution

  • has_group() needs a full external ID with module name and the ID itself.

    I would also not use == False because that is bad style.

    @api.multi
    @api.onchange('event_type')
    def _test(self):
        if self.event_type == 'HV' and\
                not self.env.user.has_group('module_name.external_id'):
            raise ValidationError('Only users with "TL" group rights ...' )