This question has been flagged
6 Replies
35337 Views

Hai to all,

 I will create a group 'Commission' through xml. then there is any way to add users to this group through the cod(python code) or csv file. this is xml file for creating group

<record model="ir.module.category" id="module_category_commission">
            <field name="name">Commission</field>
            <field name="description">Salesmen Commission Table</field>
            <field name="sequence">20</field>
        </record>

        <record id="salesmen_commission_group" model="res.groups">
            <field name="name">Commission</field>
            <field name="comment">Salesmen Commission Permission Group.</field>
            <field name="category_id" ref="module_category_commission"/>
        </record>

Avatar
Discard
Best Answer

You can set users using this python code. put that in any method and use that method. (Odoo 8)

res_groups = self.pool['res.groups']

users = self.pool['res.users'].search(cr, uid, [], context) #if you not want to set group to all user then set proper domain instead of []

group_id = self.pool['ir.model.data'].get_object(cr, uid, 'your_module_name',  'salesmen_commission_group')

res_groups.write(cr, uid, [group_id], {'users': [(4, user) for user in users]}, context=context)

Odoo13:
users = self.env['res.users'].search([]) #if you not want to set group to all user then set proper domain instead of []

group_id = self.env.ref('your_module_name.group_name')
group_id.users = [(4, user.id) for user in users]

Avatar
Discard

Thanks, It's working for me +1

Best Answer

Here is example of adding the admin user to the group you have created trough the XML:

        <record model="res.groups" id="salesmen_commission_group">
            <field name="name">Commission</field>
            <field name="comment">Salesmen Commission Permission Group.</field>
            <field name="category_id" ref="module_category_commission"/>
            <field name="users" eval="[(4, ref('base.user_root'))]"/>
        </record>

 

Avatar
Discard
Best Answer

Hi dude,

You can try with this for python

def set_commission_group(self, user_id):
commission_group = self.env.ref('your_module_name.salesmen_commission_group')
commission_group.write({'users': [(4, user_id)]})
Avatar
Discard
Best Answer

Hi, Carefull with this action you will reset the group members with users items

instead we can can just add on remove a user with + and -
usersToAdd = searching user with domain or somthing
group = env['res.groups'].search([('id', '=', GROUP_ID)])
group.write({'users' : group.users + usersToAdd})
or
group.write({'users' : group.users - usersToAdd})

Avatar
Discard
Best Answer

If you were trying to add user into an existing group with noupdate on, this might be the best and only way for you to add user into the group


<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="base.user_admin" model="res.users">
<field name="groups_id" eval="[(4, ref('account.group_account_user'))]"/>
</record>
</data>
</odoo>
Avatar
Discard
Best Answer

Hi,

in my case it worked in the following way


@api.multi
def assign_perms(self):
    users_id = [1, 2, 3, 4, 5, .........]
    aux_users = [(4, i) for i in users_id]
    group = self.env['res.groups'].search([('id', '=', self.env.ref('module_name.identifier').id)])
    group.write({'users': aux_users})

Avatar
Discard