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

时间:2016-03-21 19:29:33

标签: python python-2.7 python-3.x openerp

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

先谢谢

1 个答案:

答案 0 :(得分:2)

您应该在res.users模型中覆盖create方法,如:

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