Odoo Help


How to assign a group for user programatically in openerp 7?

on 7/10/14, 1:01 AM 2,689 views

I need to assign the groups to user through programatically rather than directly selecting the groups from Access Rights screen.

I have created a field called "user_roles" which will give the list of roles such as [Sales, Purchase,so on..].

based on these roles i need to assign groups to user, and to clear other standard groups assigned to user.

Richmond Ang
On 7/10/14, 1:46 PM

You can do something like this

user_model = self.pool.get('res.users')
external_id_model = self.pool.get('ir.model.data')
portal_group_id = external_id_model.get_object_reference(cr,uid,'portal','group_portal')[1]
res_id = False

username = "login id of user"

res_id = user_model.search(cr, uid, [('login','=', username)]))[0].id

user_model.write(cr,uid,[res_id],{'groups_id': [(6, 0, [portal_group_id])]})

this code removes all previous groups then adds the portal group.

if you need more reference on adding to many2many fields:


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/10/14, 1:01 AM
Seen: 2689 times
Last updated: 3/16/15, 8:10 AM