Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
6391 Näkymät

Hello All,

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

Avatar
Hylkää
Paras vastaus

Hello,

You can try this,

@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
            user.write({'groups_id': [(4, self.env.ref('base.group_user').id),
                                                    (4, self.env.ref('sales_team.group_sale_salesman_all_leads').id),
                                                    (4, self.env.ref('survey.group_survey_manager').id)
                                                       ]})
            # want to remove from below group
           user.write({'groups_id': [(3, self.env.ref('base.group_portal').id),
                                                    (3, self.env.ref('survey.group_survey_user').id),
                                                        ]})
    res = super(Contact, self).write(values)
    return res


You can also use like this:

    user.write({'groups_id': [(4, self.env.ref('base.group_user').id),
                                            (4, self.env.ref('sales_team.group_sale_salesman_all_leads').id),
                                            (4, self.env.ref('survey.group_survey_manager').id),
                                            (3, self.env.ref('base.group_portal').id),
                                            (3, self.env.ref('survey.group_survey_user').id),
                                           ]})


Hope this will work for you,

Thanks

Avatar
Hylkää
Tekijä

Thanks Malay, it's working fine

Paras vastaus
Hello Pawan,

To remove a user from a group, you can do it like this

your_group = self.env.ref('module.odoo_group', False)
your_group.write({'users': [(3, user_id)]})

For Adding,
 your_group = self.env.ref('module.odoo_group', False) 
 your_group.write({'users': [(4, user_id)]})
You can use loop and your logic to execute all at once.

Check this for more understanding, how to write to a One2many field.
(0, 0, { values }) 
link to a new record that needs to be created with the 
given values dictionary
(1, ID, { values }) 
update the linked record with id = ID (write values on it)
(2, ID) 
remove and delete the linked record with id = ID 
(calls unlink on ID, that will delete the object completely, 
and the link to it as well)
(3, ID) 
cut the link to the linked record with id = ID 
(delete the relationship between the two objects but does 
not delete the target object itself)
(4, ID) 
link to existing record with id = ID (adds a relationship)
(5) 
unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) 
replace the list of linked IDs (like using (5) then (4,ID) 
for each ID in the list of IDs)




Thanks & Regards
Avinash N K

Avatar
Hylkää
Tekijä

Thanks avinash, it's working

My pleasure

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
marrask. 23
921
3
marrask. 23
16441
3
marrask. 24
23507
1
huhtik. 23
5430
2
jouluk. 22
6113