This question has been flagged
4 Replies
5126 Views

I am looking to add messages whenever a change is made to a product record, so that we have a history of changes and who made them. However I cannot seem to figure out how to implement this. From what I can tell, it seems like I need a new message Subtype, but I couldn't figure out how to create one that worked. I set up the following:


Message Type: Product Change

Model: product.product

Default: 1

Description: null

Parent: null

Relation field: null


but this doesn't seem to do anything. Is there a place I can define this? The end goal is having a message whenever any field in a product.product record gets changed. Either in the GUI or via a custom module would be fine - but I can't seem to find any documentation or good examples on this. Cheers!

Avatar
Discard
Author Best Answer

Axel's answer was great for state changes, but what I wanted was field changes. Turns out this is actually pretty simple, just inherit the fields and add the parameter track_visibility='onchange'. This will set a message every time a change to that field is made. Super easy once I figured it out.

Avatar
Discard

that is another way but my way is not only for state fields, read the code that tracks fields: if visibility == 'always' or (visibility == 'onchange' and name in updated_fields) or name in self._track:

Author

Got it - thanks for clarifying. I appreciate your help!

Best Answer

You don't need to manually create messages for your field changes. There is an automatic tracking/logging system for models fields changes in OpenERP v7. like source code said at: openerp/addons/mail/mail_thread.py on the mail.thread module:

    # Automatic logging system if mail installed
# _track = {
# 'field': {
# 'module.subtype_xml': lambda self, cr, uid, obj, context=None: obj[state] == done,
# 'module.subtype_xml2': lambda self, cr, uid, obj, context=None: obj[state] != done,
# },
# 'field2': {
# ...
# },
# }
# where
# :param string field: field name
# :param module.subtype_xml: xml_id of a mail.message.subtype (i.e. mail.mt_comment)
# :param obj: is a browse_record
# :param function lambda: returns whether the tracking should record using this subtype

take as example _track properties on crm.lead and account.invoice

For crm.lead:

    _track = {
'state': {
'crm.mt_lead_create': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'new',
'crm.mt_lead_won': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'done',
'crm.mt_lead_lost': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'cancel',
},
'stage_id': {
'crm.mt_lead_stage': lambda self, cr, uid, obj, ctx=None: obj['state'] not in ['new', 'cancel', 'done'],
},
}

For account.invoice:

    _track = {
'type': {
},
'state': {
'account.mt_invoice_paid': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'paid' and obj['type'] in ('out_invoice', 'out_refund'),
'account.mt_invoice_validated': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'open' and obj['type'] in ('out_invoice', 'out_refund'),
},
}
Avatar
Discard
Author

So how would I implement this to show changes in the message section below? Adding new fields to the _track dictionary? Thanks for pointing me in the right direction.

yes, just add the needed fields to the _track dict like in the examples and you will be ok