覆盖 res.users 的创建方法以在创建时将用户添加到组中

Overwrite create method of res.users to add users upon creation to a group

我创建了一个将用户添加到自定义组的简单函数。它通过按钮工作正常但问题是每次创建新用户时我都必须单击按钮否则他将无法访问自定义模块所以我想覆盖 res.users 的创建方法并包含 add_to_group 函数。结果,如果有人通过网站注册,他们将被自动添加到组中。

这是我的代码

@api.multi
def add_to_group(self):
    group = self.env['res.groups'].search([('name','=','Applicant)]) #search for my custom group
    user_id = self.id #get the current user id
    group.users = [user_id] #add the user to the group

提前致谢

您应该重写 res.users 模型中的创建方法,如下所示:

@api.model
def create(self, vals):
    res = super(ResUsers, self).create(vals)
    res.add_to_group()
    return res