我正在使用 Odoo 11,我想在 @api.constraint 方法中显示警告弹出窗口。我要显示的警告弹窗有两个按钮,第一个是用于忽略警告的 OK 按钮,另一个是用于尝试保存操作的 Cancel 按钮,它类似于 odoo 使用的警告弹窗在下图中:
我在网上进行了很多搜索,发现了不同的建议解决方案,例如使用 Wizard、exception.Warning() 和 osv.except_osv(),但不幸的是,没有一个解决方案能完全满足我的需求。
请问有什么帮助吗?
您可以使用的基本 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}
这将返回与您想要的相同的警告向导,如给定图像所示。
我写了一个(草稿)模块打开对话框见: https ://github.com/Micronaet/micronaet-sales/tree/master/confirm_dialog_wizard
在您的按钮中,您可以编写此代码以打开,例如。隐藏产品:
@api.multi
def hide_product_pricelist(self):
""" Hide product
"""
return self.env['dialog.box.wizard'].open_dialog(
message='The product will be hided, <b>you cannot use again</b> '
'but remain in sale order where yet present, <br/>'
'confirm?',
action='self.env["product.product"].browse(%s).write('
'{"active": False})' % self.id,
title='Confirm request:',
mode='cancel_confirm',
)
该程序将弹出一个窗口进行确认(如在树中您不能使用“确认”消息参数),我正在尝试做得更好但是......它是一个开始...... :)
它类似于我们confirm
在按钮上使用属性时显示的警告。
<button confirm="Reset to draft!"/>
引发验证错误或返回警告作为字典没有显示Cancel
按钮。