为odoo中的特定组发送通知

Send notification for specific group in odoo

我想向制造组中的每个人发送通知,所以我尝试了这段代码,但它不起作用

manf_categ_ids=self.pool.get('ir.module.category').search(cr,uid,[('name','=','Manufacturing')],context=context)[0]
    users=self.pool.get('res.groups').browse(cr, uid, manf_categ_ids , context=context).users
    for user in users:
        recipient_partners = []
        recipient_partners.append(
            (4, user.partner_id.id)
        )       
    #user_ids=self.pool.get('res.users').search(cr,uid,[('groups_id','=',manf_categ_ids)],context=context)
    post_vars = {'subject': "notification about order",
         'body': "Yes inform me as i belong to manfacture group",
         'partner_ids': recipient_partners,} # Where "4" adds the ID to the list 
                                   # of followers and "3" is the partner ID 
    thread_pool = self.pool.get('mail.thread')
    thread_pool.message_post(
            cr, uid, False,
            type="notification",
            subtype="mt_comment",
            context=context,
            **post_vars)

2个用户属于制造组,但用户列表只包含1个元素的问题,当我用这个用户登录时,这段代码不发送任何通知

首先你需要使用合作伙伴的 id,而不是用户的 id。其次,您需要添加所有用户,而不仅仅是第一个用户。

这是一个基于我在项目中使用的代码,用于创建一个数组,该数组可用作 partner_ids 参数的值:

group = self.env['res.groups'].search([('category_id.name', '=', 'Manufacturing')])
recipient_partners = []
for recipient in group.users: 
    recipient_partners.append(
        (4, recipient.partner_id.id)
    )

可以看到the code this is based on here。它从 MessageTemplate 的 send_group 方法开始,并继续进入 send 方法。

您目前似乎没有使用新的 Odoo ORM API。您可以开始使用它(我强烈推荐它!)或将旧 API 所需的参数(cr、uid、context)添加到 search() 方法并使用 browse() 来获取完整的用户对象。

问题似乎是您每次迭代都清除列表。

recipient_partners = []

应该在 for 循环之外。