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

时间:2018-03-27 09:07:26

标签: python warnings odoo-11

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

我在网上搜索了很多,我找到了不同的提议解决方案,例如使用Wizard,exception.Warning()和osv.except_osv(),但不幸的是,这个解决方案没有一个能给我我想要的东西。

请帮忙吗?

4 个答案:

答案 0 :(得分:1)

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

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}

这将返回相同的警告向导,如图所示。

答案 1 :(得分:1)

我编写了一个(草稿)模块以打开对话框,请参见: https://github.com/Micronaet/micronaet-sales/tree/master/confirm_dialog_wizard

在您的按钮上,您可以编写此代码以打开,例如。隐藏产品:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    int c = 0;
    char sir[100], copie[100];
    cin.get(sir, 100);
    strcpy(copie, sir);
    char* pch = strtok(sir, " ");
    while (pch != NULL)
    {
        c++;
        pch = strtok(NULL, " ");
    }
    cout << c << endl;
    char* pch2 = strtok(copie, " ");
    while (pch2 != NULL)
    {
        if ((strchr("aeiouAEIOU", pch2[0]) && strchr("aeiouAEIOU", pch2[strlen(pch2)-1])) || (strchr("aeiouAEIOU", pch2[0] == NULL) && strchr("aeiouAEIOU", pch2[strlen(pch2)-1] == NULL)))
        {
            cout << pch2<<" ";
        }
        pch2 = strtok(NULL, " ");
    }
}

程序将弹出一个窗口进行确认(如在树中,您不能使用“ confirm”消息参数),我试图做得更好,但是...这是一个开始...:)

答案 2 :(得分:0)

  
    

如果不满足其不变量,则该方法会引发异常。

  

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

AccessDenied
RedirectWarning
MissingError
except_orm
AccessError
DeferredException
ValidationError
Warning

答案 3 :(得分:0)

可以使用的基本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))

这应该可以解决您的问题。