Odoo - 提出 javascript 警告

Odoo - raise javascript warning

如何在 Odoo 中发出 javascript 警告?

/addons/web_view_editor/static/src/js/view_editor.js中有例子:

this.do_warn(_t("The following fields are invalid :"), msg)

如何通过on_change方法调用类似函数,例如在python?

Onchange 方法可以 return 具有标准结构的字典,将由 Web 客户端解释。 Here you can read the return format, in particular:

Onchange methods can show errors and/or change fields domain/values by returning a dictionary with one of more of these keys:

warning
Used to show an error popup, useful for example for alerting the user that the value he inserted is invalid. The value should be a dict in the form {'title': , 'message': } where will be the title of the error popup and the error message.
[...]

因此您可以通过 returning 引发警告对话框,例如:

return {
    'warning': {
        'title': 'Invalid value',
        'message': 'The field percentage must be an integer between 0 and 100'
    }
}

您不能从 onchange 方法(后端 python 代码)调用和执行任意 javascript 函数,您只能使用 return 字典与网络客户端交互。

This answer in a similar question reference you can return:

{
'type': 'ir.actions.client',
'tag': 'action_warn',
'name': 'Warning',
'params': {
   'title': 'Warning!',
   'text': 'Entered Quantity is greater than quantity on source.',
   'sticky': True
}
}

像ODOO一样显示消息。希望对您有所帮助。