Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
6 ตอบกลับ
38462 มุมมอง

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>

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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]

อวตาร
ละทิ้ง

Thanks, It's working for me +1

คำตอบที่ดีที่สุด

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>

 

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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)]})
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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})

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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>
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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})

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
เม.ย. 25
3342
3
ธ.ค. 22
11188
How to get current url from bowser? แก้ไขแล้ว
5
เม.ย. 24
41575
how to compute Len() of list python แก้ไขแล้ว
3
มี.ค. 24
10267
2
ก.ค. 19
765