Search code examples
pythonpython-2.7odooodoo-10odoo-view

Why does not api.depends work with the message_follower_ids field on non edit mode? How to fix it?


I want to show a message when I have follower in the document and they are not users of Odoo. So I created two computed fields, one for the message and another one to check if the message should be shown.

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.multi
    @api.depends('message_follower_ids')
    def _compute_show_warning_msg(self):
        partner_ids = self.env['res.users'].search([])\
            .mapped('partner_id').mapped('id')
        for record in self:
            msg = _("""\nDocument contains no user followers.\n""")
            for follower in record.message_follower_ids:
                if follower.partner_id.id not in partner_ids:
                    record.show_warning_msg = True
                    record.warning_msg = msg

    show_warning_msg = fields.Boolean(
        compute='_compute_show_warning_msg',
        string='Show warning message',
    )

    warning_msg = fields.Text(
        compute='_compute_show_warning_msg',
        string='Warning message',
    )

But when the form is not in edit mode and I add or remove followers the compute method is not triggered. So I must press the "Edit" button and the "Save" to trigger the method.

I have tried to execute the compute method on the create and unlink methods of the follower table, but the form view is not refreshed

Is there a way to trigger the compute field and refresh the form in order to show the message?

Any workaround or fix?


Solution

  • A possible solution, I added this code to the form view:

    <field name="message_follower_ids" position="attributes">
        <attribute name="class">oe_edit_only</attribute>
    </field>