如何在Odoo-11中显示警告信息?

2024-05-14 01:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Odoo 11,我想在@api.constraint方法中显示一个警告弹出窗口。我要显示的警告弹出窗口有两个按钮,第一个是用于忽略警告的“确定”按钮,另一个是用于尝试保存操作的“取消”按钮,它类似于下面图片中odoo使用的警告弹出窗口: The warning message that i want to display is similar to this one

我在网上搜索了很多,发现了不同的解决方案,比如使用Wizard、except I on.Warning()和osv.except_osv(),但不幸的是,没有一个解决方案能完全满足我的要求。

有什么帮助吗?


Tags: 方法odooapi警告on图片解决方案按钮
3条回答

您可以使用的基本odoo警告是从odoo.exception类调用的。 例如:

from odoo.exceptions import AccessError, UserError, RedirectWarning, ValidationError, Warning

@api.constrains('age')
def _check_something(self):
    for record in self:
        if record.age > 20:
            raise Warning(_("Your record is too old: %s" % record.age))

这应该能解决你的问题。

你可以用不同的方式发出警告信息。我已经通过这种方式创建了与库存数量相关的消息:

if self.qty > new_qty:
       message = _('You plan to sell %s quantity but you only have %s available in %s warehouse.') % \
                     (self.qty, self.product_id.virtual_available, self.order_id.warehouse_id.name)
       mess= {
                    'title': _('Not enough inventory!'),
                    'message' : message
                }
       return {'warning': mess}

此向导返回的警告与您所需的相同,如给定图像所示。

The method is expected to raise an exception if its invariant is not satisfied.

Odoo提供了一个预定义的异常,您可以使用:

AccessDenied
RedirectWarning
MissingError
except_orm
AccessError
DeferredException
ValidationError
Warning

相关问题 更多 >