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)