我的需求不是销售不同类别的产品,所以我决定覆盖create方法:
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记录,这很好,但不显示弹出窗口错误,我需要此弹出窗口被显示。
是否有人正确处理了Odoo9中的机智异常(经典的raise osv.except_osv被破坏了)?
提前感谢!
发布于 2021-04-21 16:46:22
try:
print 100 / 0
raise UserError(_('foo'))
except Exception as e:
raise UserError(_(str(e)))https://stackoverflow.com/questions/36135780
复制相似问题