我想在创建记录时向关注者发送自定义消息我的班级:
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
的定义,可以获得更多可能的参数,如subject
或partner_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