Skip to Content
मेन्यू
This question has been flagged
2 Replies
415 Views

I have this defined in my equipment:


class AssetEquipment(model.Models):
    _name = "asset.tracking"
    _description = "Module to track asset movement"

    scrap_date = fields.Date('Scrap date')
    owner_id = fields.Many2one('res.users', string='Owner', default=False, tracking=True)
    status_id = fields.Selection([   
        ('0', 'Inactive'),
        ('1', 'Active'),
        ('2', 'Under Repair'),
        ('3', 'Scrap'),
    ], string='Status', default='0', store=False, compute='_compute_status')

Now, I need to display a list of owner_id change in tracking value, along with the date, but I want to put it inside notebook Something like


<field name="owner_history">
    <list>
        <field name="date" />
        <field name="owner_id" />
        <field name="status" />
    </list>
</field>


How can I do that? Thank you

Avatar
Discard
Best Answer

Hi,

Please refer to the code:

from odoo import models, fields, api


class AssetTracking(models.Model):

    _name = "asset.tracking"

    _inherit = ['mail.thread']

    _description = "Asset Tracking"


    owner_id = fields.Many2one('res.users', string='Owner', tracking=True)


    def write(self, vals):

        for rec in self:

            old_owner = rec.owner_id

            res = super().write(vals)

            if 'owner_id' in vals:

                new_owner = self.env['res.users'].browse(vals['owner_id'])

                rec.message_post(

                    body=f"Owner changed from <b>{old_owner.name if old_owner else 'None'}</b> "

                         f"to <b>{new_owner.name}</b>"

                )

        return res


This code defines an Odoo model called asset.tracking that inherits from mail.thread, which means it can use the chatter to log messages. The model has a field owner_id to store the current owner of the asset, and tracking=True ensures Odoo automatically tracks changes to this field.


The write method is overridden so that whenever a record is updated, it first saves the current owner (old_owner), then calls the original write to apply the changes. If the owner_id field is being changed, it posts a message in the chatter showing who the previous owner was and who the new owner is. This creates a clear, readable log of ownership changes directly in the record’s chatter.


Hope it helps.


Avatar
Discard
Best Answer

Hello  Rudi,


I faced the same issue and was able to solve it using the write method.

In asset tracking, you can use the ORM write method like this:


Python

#Code in comment//


Here, replace "field_id" with the actual Many2one field in your model that corresponds to the owner_history relation  model.

If you have any questions, feel free to reach out.

Hope this helps!


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

Avatar
Discard

Code :

def write(self, vals):
res = super(AssetEquipment, self).write(vals)
self.ensure_one()
if "owner_id" in vals and vals["owner_id"]:
message = "Updated owner to '%s'." % self.owner_id.name
self.field_id.message_post(body=message)
return res