Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

I want to track changes in records of my One2many fields. I've tried the following code:

@api.model_create_multi

    def create(self, vals_list):

        records = super().create(vals_list)

        for record in records:

            if record.lead_id:

                message = f"New Secondary Salesman Added: {record.name}"

                record.lead_id.message_post(body=message, **{'body_is_html': True})

        return records


    def write(self, vals):

        # Store original values before the write operation

        old_values = {}

        for field_name, value in vals.items():

            if field_name in self._fields and self._fields[field_name].tracking:

                # For basic fields (Char, Float, Integer, etc.), directly store the old value

                # For Many2one, you might want to store the display_name or ID

                if self._fields[field_name].relational:

                    old_values[field_name] = self[field_name].display_name if self[field_name] else False

                else:

                    old_values[field_name] = self[field_name]



        res = super().write(vals)


        for record in self:

            if record.lead_id:

                body_message = f"<b>Changes to Secondary Salesman info '{record.display_name}':</b><br/>"

                has_changes = False

                for field_name, new_value in vals.items():

                    if field_name in record._fields and record._fields[field_name].tracking:

                        old_value = old_values.get(field_name)

                        current_value = record[field_name]


                        # Handle different field types for comparison and display

                        if record._fields[field_name].relational:

                            current_display_value = current_value.display_name if current_value else False

                            if str(old_value) != str(current_display_value): Compare display names or IDs

                                has_changes = True

                                field_description = record._fields[field_name].string

                                body_message += f"<li>{old_value or ''} to {current_display_value or ''} ({field_description})<br/>"

    #                     elif str(old_value) != str(current_value): # For non-relational fields

    #                         has_changes = True

    #                         field_description = record._fields[field_name].string

    #                         body_message += f"<li>{old_value or ''} to {current_value or ''} ({field_description}) <br/>"


    #             if has_changes:

    #                 record.lead_id.message_post(body=body_message, **{'body_is_html': True})

    #     return res

    # def unlink(self):

    #     for record in self:

    #         if record.lead_id:

    #             message = f"<b>Salesman Deleted:</b> {record.display_name}"

    #             record.lead_id.message_post(body=message, **{'body_is_html': True})

    #     return super().unlink()

While this works well for one model, when I add this to multiple models, I get the following error either while creating a new record or updating an existing one.

UncaughtPromiseError > OwlError


Uncaught Promise > An error occured in the owl lifecycle (see this Error's "cause" property)


Occured on localhost:8069 on 2025-05-26 10:41:28 GMT


OwlError: An error occured in the owl lifecycle (see this Error's "cause" property)

    Error: An error occured in the owl lifecycle (see this Error's "cause" property)

        at handleError (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:961:101)

        at App.handleError (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:1608:29)

        at RootFiber.complete (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:993:28)

        at Scheduler.processFiber (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:1578:43)

        at Scheduler.processTasks (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:1572:62)

        at http://localhost:8069/web/assets/f129831/web.assets_web.min.js:1569:67


Caused by: NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.

    at VMulti.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:796:116)

    at B.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:881:178)

    at VMulti.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:795:224)

    at VList.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:902:88)

    at B.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:881:178)

    at VToggler.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:721:78)

    at VList.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:902:88)

    at VMulti.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:795:224)

    at VMulti.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:795:224)

    at VToggler.patch (http://localhost:8069/web/assets/f129831/web.assets_web.min.js:721:78)

What is causing the error? OR 
How can I add tracking information to my form's chatter?


Odoo v18 Community edition

Awatar
Odrzuć
Najlepsza odpowiedź


This error is not actually caused by the Python backend logic itself (your create, write, unlink methods), but by how the frontend (Owl JS framework in Odoo) is attempting to render the result of the operation, especially if a view or widget is affected in a way it wasn't expecting.Add this at the top of your file:
from odoo.tools import html_escape

Then sanitize your HTML message in the write method:

Replace:

body_message += f"<li>{old_value or ''} to {current_display_value or ''} ({field_description})<br/>"

with:
body_message += f"<li>{html_escape(str(old_value) or '')} → {html_escape(str(current_display_value) or '')} ({html_escape(field_description)})</li>"



Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
lip 25
479
1
cze 25
669
1
cze 25
836
1
sie 21
3901
1
lip 25
548