如何使用 Python 代码在 Odoo 15 中创建相关用户
How to Create a Related User in Odoo 15 with Python Code
美好的一天,
我正在寻求帮助。
我正在尝试使用 python 代码在 Odoo 15 中创建一个用户。我希望当 HR 创建 Employee 时,相关用户也会被创建。此相关用户应属于内部用户组。下面的代码给我“用户不能有超过一种用户类型。”
class Doctors(models.Model):
_inherit = "hr.employee"
@api.model
def create(self, values):
employee = super(Doctors, self).create(values)
company_id = self.company_id or self.env.user.sudo().company_id
if employee.work_email and not employee.user_id:
employee.sudo().create_employee_related_user()
return employee
def create_employee_related_user(self):
for record in self:
company_id = record.company_id or self.env.user.sudo().company_id
if not record.work_email:
raise UserError(_('Please define valid email for the Employee'))
group_user = self.env.ref('base.group_user') or False
user = record.user_id[0] if record.user_id else None
# update partner email, if a new one was introduced also add internal group to relative user of selected partners
user_internal = None
# create a user and make sure it is in the internal group
if not user:
user_internal = record.sudo().with_context(company_id=company_id)._create_user()
else:
user_internal = user
if group_user not in user_internal.groups_id:
user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
def _create_user(self):
company_id = self.env.context.get('company_id')
get_user = self.env['res.users'].sudo().search([('email','=',self.work_email)], limit=1)
if get_user:
raise UserError(_('Employee/User already registered with given email address.'))
return self.env['res.users'].with_context(no_reset_password=True)._create_user_from_template({
'email': self.work_email,
'login': self.work_email,
'name': self.name,
'company_id': company_id.id,
'company_ids': [(6, 0, [company_id.id])],
})
替换下面的代码行:
user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
有
user_internal.write({'active': True, 'groups_id': [(6, 0, [group_user.id)]})
这将替换所有用户组并将其添加到内部用户组。
使用默认用户模板:
顺便说一句,您正在使用 _create_user_from_template
方法从默认用户模板创建用户,您可以通过转到用户和过滤器(Active = False)并打开默认用户模板并更改默认用户模板来编辑默认用户模板用户类型 Internal User
以及应用程序权限。这将帮助您创建具有您在默认用户模板中分配的应用程序权限的内部用户。
因此您可以从代码中删除以下行:
if group_user not in user_internal.groups_id:
user_internal.write({'active': True, 'groups_id': [(4,group_user.id)]})
不使用默认用户模板创建用户
如果您想在不使用默认用户模板的情况下创建一个对任何应用程序没有任何权限的用户(内部用户),您可以按如下方式进行:
class Doctors(models.Model):
_inherit = "hr.employee"
@api.model
def create(self, values):
employee = super(Doctors, self).create(values)
company_id = self.company_id or self.env.user.sudo().company_id
if employee.work_email and not employee.user_id:
employee.sudo().create_employee_related_user()
return employee
def create_employee_related_user(self):
for record in self:
company_id = record.company_id or self.env.user.sudo().company_id
if not record.work_email:
raise UserError(_('Please define valid email for the Employee'))
group_user = self.env.ref('base.group_user') or False
# You already check that the employee not linked to a user in Create method
#user = record.user_id[0] if record.user_id else None
# update partner email, if a new one was introduced also add internal group to relative user of selected partners
user_internal = None
# create a user and make sure it is in the internal group
#if not user:
user_internal = record.sudo().with_context(company_id=company_id)._create_user()
#else:
# user_internal = user
# No need for that, I already assigned groups in _create_user method
#if group_user not in user_internal.groups_id:
# user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
def _create_user(self):
company_id = self.env.context.get('company_id')
get_user = self.env['res.users'].sudo().search([('email','=',self.work_email)], limit=1)
if get_user:
raise UserError(_('Employee/User already registered with given email address.'))
return self.env['res.users'].with_context(no_reset_password=True).sudo().create({
'email': self.work_email,
'login': self.work_email,
'name': self.name,
'company_id': company_id.id,
'company_ids': [(6, 0, [company_id.id])],
'groups_id': [(6, 0, [self.env.ref('base.group_user').id])],
})
美好的一天, 我正在寻求帮助。
我正在尝试使用 python 代码在 Odoo 15 中创建一个用户。我希望当 HR 创建 Employee 时,相关用户也会被创建。此相关用户应属于内部用户组。下面的代码给我“用户不能有超过一种用户类型。”
class Doctors(models.Model):
_inherit = "hr.employee"
@api.model
def create(self, values):
employee = super(Doctors, self).create(values)
company_id = self.company_id or self.env.user.sudo().company_id
if employee.work_email and not employee.user_id:
employee.sudo().create_employee_related_user()
return employee
def create_employee_related_user(self):
for record in self:
company_id = record.company_id or self.env.user.sudo().company_id
if not record.work_email:
raise UserError(_('Please define valid email for the Employee'))
group_user = self.env.ref('base.group_user') or False
user = record.user_id[0] if record.user_id else None
# update partner email, if a new one was introduced also add internal group to relative user of selected partners
user_internal = None
# create a user and make sure it is in the internal group
if not user:
user_internal = record.sudo().with_context(company_id=company_id)._create_user()
else:
user_internal = user
if group_user not in user_internal.groups_id:
user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
def _create_user(self):
company_id = self.env.context.get('company_id')
get_user = self.env['res.users'].sudo().search([('email','=',self.work_email)], limit=1)
if get_user:
raise UserError(_('Employee/User already registered with given email address.'))
return self.env['res.users'].with_context(no_reset_password=True)._create_user_from_template({
'email': self.work_email,
'login': self.work_email,
'name': self.name,
'company_id': company_id.id,
'company_ids': [(6, 0, [company_id.id])],
})
替换下面的代码行:
user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
有
user_internal.write({'active': True, 'groups_id': [(6, 0, [group_user.id)]})
这将替换所有用户组并将其添加到内部用户组。
使用默认用户模板:
顺便说一句,您正在使用 _create_user_from_template
方法从默认用户模板创建用户,您可以通过转到用户和过滤器(Active = False)并打开默认用户模板并更改默认用户模板来编辑默认用户模板用户类型 Internal User
以及应用程序权限。这将帮助您创建具有您在默认用户模板中分配的应用程序权限的内部用户。
因此您可以从代码中删除以下行:
if group_user not in user_internal.groups_id:
user_internal.write({'active': True, 'groups_id': [(4,group_user.id)]})
不使用默认用户模板创建用户
如果您想在不使用默认用户模板的情况下创建一个对任何应用程序没有任何权限的用户(内部用户),您可以按如下方式进行:
class Doctors(models.Model):
_inherit = "hr.employee"
@api.model
def create(self, values):
employee = super(Doctors, self).create(values)
company_id = self.company_id or self.env.user.sudo().company_id
if employee.work_email and not employee.user_id:
employee.sudo().create_employee_related_user()
return employee
def create_employee_related_user(self):
for record in self:
company_id = record.company_id or self.env.user.sudo().company_id
if not record.work_email:
raise UserError(_('Please define valid email for the Employee'))
group_user = self.env.ref('base.group_user') or False
# You already check that the employee not linked to a user in Create method
#user = record.user_id[0] if record.user_id else None
# update partner email, if a new one was introduced also add internal group to relative user of selected partners
user_internal = None
# create a user and make sure it is in the internal group
#if not user:
user_internal = record.sudo().with_context(company_id=company_id)._create_user()
#else:
# user_internal = user
# No need for that, I already assigned groups in _create_user method
#if group_user not in user_internal.groups_id:
# user_internal.write({'active': True, 'groups_id': [(4, group_user.id)]})
def _create_user(self):
company_id = self.env.context.get('company_id')
get_user = self.env['res.users'].sudo().search([('email','=',self.work_email)], limit=1)
if get_user:
raise UserError(_('Employee/User already registered with given email address.'))
return self.env['res.users'].with_context(no_reset_password=True).sudo().create({
'email': self.work_email,
'login': self.work_email,
'name': self.name,
'company_id': company_id.id,
'company_ids': [(6, 0, [company_id.id])],
'groups_id': [(6, 0, [self.env.ref('base.group_user').id])],
})