-->

OpenERP 7 - Create auto log message in “Log a Note

2019-07-28 02:02发布

问题:

I am now creating a customize module for a lab test purpose in OpenERP 7. The users will require to enter their lab test components and result here.

Now i having a field which called "Reason For Changes". I would like to know the method how i can log the input of this content as a "log a note" message to display at the bottom through mail.thread?

The step would be:

  1. Reason For Changes (ROC) as a required field

  2. Any changes in my others fields will calling my onchange method to clearing the content of ROC field.

  3. If the user changed something without enter a text to the ROC field, then click save, a error message "Please Enter Reason For Change" will pop up. This will disable the user from saving this.

  4. If the user changed something and enter a text to ROC field, then save, the ROC field content will create as a message at the bottom (such as "log a note") as a reference and history log record.

My question would be how could i achieve the step 3 and 4? Deeply appreciate for your help

回答1:

There are two possiblities, but the used model has to inherit email.thread! But i guess it is inheriting, because you wrote something about the chatter messages.:

  1. Use Odoo's (former OpenERP) automatic tracking system. You just have to add the Parameter track_visibility on your field definition, like (new then old API)
roc = fields.Char(string="Reason For Changes", track_visibility="on_change")

_columns = {
    roc: fields.char(string="Reason For Changes", track_visibility="on_change"),
}
  1. Post a message on write by yourself. email.thread is coming with some simple yet useful methods. One of them is message_post(). Override the write() of the model, like the following (new/old API):
@api.multi
def write(self, vals):
    res = super(YourModel, self).write(vals)
    if 'roc' in vals:
        for your_model_record in self:
            your_model_record.message_post(vals.get('roc'))
    return res


def write(self, cr, uid, ids, vals, context=None):
    res = super(YourModel, self).write(vals)
    if 'roc' in vals:
        for your_model_record_id in ids:
        self.message_post(cr, uid, your_model_record_id, vals.get('roc')), context=context)
    return res


回答2:

I hope my question could help others as well. Follow would be the complete code example that working to me now:

  def write(self, cr, uid, ids, vals, context=None):
      res = super(test_lab, self).write(cr, uid, ids, vals, context=context)        
      if 'ROC' in vals: 
          for lab in self.browse(cr, uid, ids, context=context):               
              self.message_post(cr,uid,[lab.id],vals.get('ROC'),context=context)                
      return res   

Hope this is helpful. Thanks!