在od中发送特定组的通知

2024-05-15 23:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想给制造组的每个人发一份通知,所以我试过这个代码,但它不起作用

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个元素,当我使用此用户登录时,此代码不发送任何通知


Tags: selfididspartneruidgetcontextusers
2条回答

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

线路

recipient_partners = []

应该在for循环之外。

首先,您需要使用合作伙伴的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()获取完整的用户对象。

相关问题 更多 >