odoo 如何创建消息追踪

1、实现消息记录

当视图上的某个字段发生修改时,我们希望在视图的底部记录是哪个用户什么时候对它进行了修改,实现效果如下所示:

主要实现是在模型定义的时候,继承'mail.thread', 'mail.activity.mixin'这两个模型,

前端xml代码:

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

python 代码:

class MarketCooperation(models.Model):
    _name = 'market.cooperation'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    def get_currency(self):
        return self.env['res.currency'].search([('name', '=', 'CNY')]).id

    name = fields.Char(string="合作名")
    market_partner_id = fields.Many2one("res.partner", domain=[("is_marking", "=", True), ("parent_id", "=", False)],
                                        string="合作伙伴", tracking=True)

 主要是通过tracking属性对消息追踪进行控制

2、自定义按钮发送消息

<group name="chatter_functionalities">
<button string="Generate internal message" type="object" name="generate_internal_message"></button>
<button string="Generate message" type="object" name="generate_message"></button>
<button string="Generate activity" type="object" name="generate_activity"></button>
</group> 

按钮所对应的函数:

 

def generate_internal_message(self):
# This function will generate an internal message
 self.env['mail.message'].create({
'subject': 'Example internal message for contact ' + self.contact_id.name,
'message_type': 'comment',
'body': 'This internal message is automatically generated',
# This type defines that it is an internal note (the boolean 'internal only' for the mail.message.subtype
# record 'Note' is set to true)
'subtype_id': self.env.ref('mail.mt_note').id,
# Current model and current record id
'model': 'contact.note',
'res_id': self.id
 })

def generate_message(self):
# This function will generate an chatter message (not internal)
 self.env['mail.message'].create({
'subject': 'Example message for contact ' + self.contact_id.name,
'message_type': 'comment',
'body': 'This message is automatically generated',
'subtype_id': self.env.ref('mail.mt_comment').id,
# Current model and current record id
'model': 'contact.note',
'res_id': self.id
 })

def generate_activity(self):
# Will create an activity in the model mail.activity.
 self.activity_schedule(
'mail.mail_activity_data_meeting',
 summary='This is an automatically generated activity',
 note='Some meeting you have to attend',
# Plans the activity 5 days in the future
 date_deadline=datetime.datetime.today() + datetime.timedelta(days=5),
# Sets the current (logged in) user as responsible on the activity.
 user_id=self.env.user.id)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值