Skip to Content
Menu
This question has been flagged
4 Replies
5362 Views

Hello guys,

    I added Tracking for many2one and many2many fields, it works fine for many2one but it dosen't works for many2many field.Can anyone post solution for this?


Regards,

OdooPlanet

Avatar
Discard
Best Answer

Hi,

For many2many fields we cannot enable tracking just like other fields. But you can use message post function for this, which will allow you to post message in to chatter from code

Please refer this link:Odoo Message Post Function || Add Message To Chatter From Code in Odoo
 
Thank you

Avatar
Discard
Best Answer

Add tracking for many2many and one2many field in odoo16 is here:

from odoo import fields, Command


field_name = fields.Many2many('comodel_name', tracking=True)


Add the bellow function :

def _mail_track(self, tracked_fields, initial_values):
changes, tracking_value_ids = super()._mail_track(tracked_fields, initial_values)
# Many2many tracking
if len(changes) > len(tracking_value_ids):
for changed_field in changes:
if tracked_fields[changed_field]['type'] in ['one2many', 'many2many']:
field = self.env['ir.model.fields']._get(self._name, changed_field)
vals = {
'field': field.id,
'field_desc': field.field_description,
'field_type': field.ttype,
'tracking_sequence': field.tracking,
'old_value_char': ', '.join(initial_values[changed_field].mapped('name')),
'new_value_char': ', '.join(self[changed_field].mapped('name')),
}
tracking_value_ids.append(Command.create(vals))
return changes, tracking_value_ids

Thanks!

Avatar
Discard
Best Answer

One2many_tracking in odoo apps store 

Avatar
Discard
Best Answer

models.py:

# -*- coding: utf-8 -*-
from openerp import models, fields, api
# Chef Section  classchef(models.Model):    _name = 'restaurantsystemreve.chef'    _rec_name = 'firstname'        chefid = fields.Char(string='Chef ID', required=True)    firstname = fields.Char(string='First Name', required=True)    lastname = fields.Char(string='Last Name', required=True)    phoneno = fields.Char(string='Phone', required=True)    orderid = fields.Many2one('restaurantsystemreve.order', string="Oder ID", help="Oder ID", ondelete="restrict")


 

Avatar
Discard