odoo开发笔记 -- 异常、错误、警告、提示、确认信息显示
1.检查业务逻辑中的错误,终止代码执行,显示错误或警告信息:
raise osv.except_osv(_('Error!'), _('Error Message.'))
示例代码:
#删除当前销售单,需要验证销售单的状态 def unlink(self, cr, uid, ids, context=None): for rec in self.browse(cr, uid, ids, context=context): if rec.state not in ['draft']: raise osv.except_osv(_(u'警告!'),_(u'您不能删除以下状态的销售单 %s .')%(rec.state)) if rec.create_uid.id != uid: raise osv.except_osv(_(u'警告!'),_(u'您不能删除他人创建的单据.')) return super(sale, self).unlink(cr, uid, ids, context)
2.字段的 onchange 事件中返回值,同时返回提示信息:
warning = { 'title': _('Warning!'), 'message' : _('Warning Message.') } return {'warning': warning, 'value': value}
示例代码:
@api.onchange('partner_id') def onchange_partner_id_warning(self): if not self.partner_id: return warning = {} title = False message = False partner = self.partner_id # If partner has no warning, check its company if partner.sale_warn == 'no-message' and partner.parent_id: partner = partner.parent_id if partner.sale_warn != 'no-message': # Block if partner only has warning but parent company is blocked if partner.sale_warn != 'block' and partner.parent_id and partner.parent_id.sale_warn == 'block': partner = partner.parent_id title = ("Warning for %s") % partner.name message = partner.sale_warn_msg warning = { 'title': title, 'message': message, } if partner.sale_warn == 'block': self.update({'partner_id': False, 'partner_invoice_id': False, 'partner_shipping_id': False, 'pricelist_id': False}) return {'warning': warning} if warning: return {'warning': warning}
3.视图中 button 按钮点击时显示确认信息:【直接加上"confirm"属性,就可以实现,点击按钮的时候,弹出窗口,提示“是否确认操作”的提示框】
<button name="cancel_voucher" string="Cancel Voucher" type="object" states="posted" confirm="Are you sure you want to unreconcile this record?"/>
有兴趣的小伙伴可以研究下,odoo的几种抛出异常的方式的区别:
from odoo.exceptions import except_orm, Warning, RedirectWarning,AccessDenied
AccessDenied RedirectWarning MissingError except_orm AccessError DeferredException ValidationError Warning
相关参考:
http://www.cnblogs.com/cnshen/p/3205405.html