Odoo - 我如何向关注者发送自定义消息

Odoo - How can i send a custom message to followers

我想在创建记录时向关注者发送自定义消息 我的 class :

class StockAlert(models.Model):
    _name = "stock.alert"
    _inherit = ['mail.thread', 'mail.activity.mixin']

    responsable_id = fields.Many2one('res.users' ,store=True)

    @api.multi
    def write(self, vals):
      result = super(StockAlert, self).write(vals)
      for record in self:
          followers= []
          if record.responsable_id.partner_id.id not in record.message_follower_ids.ids:
              followers.append(record.responsable_id.partner_id.id)
          record.message_subscribe(followers)
      return result

    @api.model
    def create(self, vals):
      result = super(StockAlert, self).create(vals)
      for record in result:
          followers= []
          if record.responsable_id.partner_id.id not in record.message_follower_ids.ids:
              followers.append(record.responsable_id.partner_id.id)
          record.message_subscribe(followers)
      return result

根据我的看法打电话给喋喋不休的人:

<div class="oe_chatter">
            <field name="message_follower_ids" widget="mail_followers"/>
             <field name="activity_ids" widget="mail_activity"/>
            <field name="message_ids" widget="mail_thread"/>          
          </div>

请帮忙!

我不完全确定这样的关注者订阅是否正确,但发布消息应该很容易。订阅后立即执行:

record.message_post(body)  # body should be a string

查看 message_post 的定义以获得更多可能的参数,例如 subjectpartner_ids

partner_ids 对您来说可能很有趣,因为此参数可用于自动订阅 message_post。例如 create:

@api.model
def create(self, vals):
    result = super().create(vals)
    for record in result:
        partner_ids = record.responsable_id.partner_id.ids
        body = 'hello world'
        record.with_context(mail_post_autofollow=1).message_post(
            body, partner_ids=partner_ids)
    return result