# -*- coding: utf-8 -*- from odoo import api, models, _ class MailThread(models.AbstractModel): _inherit = 'mail.thread' def unlink(self): """ Override unlink to create a persistent audit log record before deletion. The audit log will have res_id=0 to ensure it survives the standard unlink process. """ audit_models = ( 'account.move', 'account.account', 'account.tax', 'res.partner', 'res.company', 'mrp.production', 'mrp.bom', 'stock.picking', 'stock.move', 'product.template', 'product.product', 'purchase.order', 'sale.order', 'hr.employee', 'product.category', 'stock.lot' ) if self._name in audit_models: for record in self: # Get basic info while record still exists record_id = record.id record_name = record.display_name or _('Unknown Record') company_id = getattr(record, 'company_id', self.env.company).id # Create the persistent audit log self.env['mail.message'].sudo().create({ 'model': self._name, 'res_id': 0, # Persistent ID (not linked to an existing record) 'message_type': 'notification', 'subject': _('Record Unlinked: %s', record_name), 'body': _( '

The following record was unlinked by %s:

' '', self.env.user.name, self._name, record_id, record_name ), 'author_id': self.env.user.partner_id.id, 'record_company_id': company_id, }) return super(MailThread, self).unlink()