I want to remove the user from multiple groups at once when changing from an 'individual' to 'company' on write method of a contact form.
@api.multi
def write(self, values):
user = self.env['res.users'].search([('partner_id', '=', self.id)])
if 'company_type' in values:
if values["company_type"] == "company":
# want to add in below group
"""[(6, 0, [self.env.ref('base.group_user').id,
self.env.ref('sales_team.group_sale_salesman_all_leads').id,
self.env.ref('survey.group_survey_manager').id
])]"""
# want to remove from below group
"""[(6, 0, [self.env.ref('base.group_portal').id,
self.env.ref('survey.group_survey_user').id,
])]"""
res = super(Contact, self).write(values)
return res
Thanks in advance
Actually you want to remove some groups and add some other groups, first when you use api.multi
always expect that the self contains more than one record, so when you do this self.id
you will probably have the famous singleton Error
.
second don't start searching for users only if you are going to do something with them, so move the search inside the if statement
.
@api.multi
def write(self, values):
if values.get('company_type', False) == "company":
users = self.env['res.users'].search([('partner_id', 'in', self.ids)])
# remove some groups: [(3, id_of_group), ....etc]
to_remove = [(3, self.env.ref(group_xml_id).id) for group_xml_id in ['base.group_portal', 'survey.group_survey_user']]
# add some groups [(4, id_of_group), ... ect]
to_add = [(4, self.env.ref(group_xml_id).id) for group_xml_id in ['base.group_user',
'sales_team.group_sale_salesman_all_leads',
'survey.group_survey_manager']]
# contatenate the list to call write only one time
users.write({'groups_id': to_remove + to_add})
return super(Contact, self).write(values)
When you want to remove record from many2many field use command
(3, id_of_record)
, and when you want to add a record (4, id_of_record)
:
Odoo ORM API documentation:
One2many and Many2many use a special “commands” format to manipulate the set of records stored in/associated with the field.
This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. Not all commands apply in all situations. Possible commands are:
(0, _, values):
adds a new record created from the provided value dict.
(1, id, values):
updates an existing record of id id with the values in values. Can not be used in create().
(2, id, _):
removes the record of id id from the set, then deletes it (from the database). Can
not be used in create().
(3, id, _):
removes the record of id id from the set, but does not delete it. Can not be used on One2many. Can not be used in create().
(4, id, _) :
adds an existing record of id id to the set. Can not be used on One2many.
(5, _, _):
removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().
(6, _, ids):
replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.