Skip to main content
 首页 » 编程设计

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

2024年10月24日10mfrbuaa

我创建了一个将用户添加到自定义组的简单函数。它通过按钮工作正常,但问题是每次创建新用户时我都必须单击按钮,否则他将无法访问自定义模块,所以我想覆盖 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