如何处理odoo 9中的异常?

时间:2016-03-21 15:54:10

标签: exception odoo-9

我的要求是不销售不同类别的产品,所以我决定覆盖create method:

from openerp import api
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.exceptions import UserError, ValidationError
class sale_order(osv.osv):
    _inherit = "sale.order"
@api.model
def create(self, vals):
    product_ids=[]
    product_categ_ids=[]
    if vals.get('name', 'New') == 'New':
        if vals.get('order_line'):
            order_lines_vals=vals['order_line']  
            for l in order_lines_vals:
                for value in l:
                    if isinstance(value, dict):
                        product_ids.append(value['product_id'])
        if product_ids:
            for j in self.env['product.product'].browse(product_ids):
                product_categ_ids.append(j.product_tmpl_id.categ_id.id)
            product_categ_ids=list(set(product_categ_ids))
            if len(product_categ_ids) > 1:
                raise ValidationError(_("It is not possible to add products belonging to many categories(Only one category is allowed)!"))


    result = super(SaleOrder, self).create(vals)
    return result

此代码不允许创建sale.order记录,这很好,但不显示弹出窗口错误,我需要显示此弹出窗口。

在odoo 9中是否正确处理了任何探测器(经典的加速osv.except_osv被取消)?

提前致谢!