In my student module I inherited res.partner. And I want to restrict to modify name in partner view if partner is a student. here is my cord.
from openerp.osv import osv, fields
class res_partner(osv.Model):
_inherit = 'res.partner'
_columns = {
'name': fields.char('Name', size=250, required=True, select=True),
'is_student': fields.boolean('Student', readonly=True),
}
def write(self, cr, uid, ids, vals, context=None):
stud = self.browse(cr, uid, ids, context=context)[0]
if ('name' in vals) & (stud.is_student is True):
raise "Cannot Edit"
else:
return super(res_partner, self).write(cr, uid, ids, vals, context=context)
it is working but I want to display error message in a message box. not a exception. How could I do this?
raise osv.except_osv(('Error'), ('Error Cannot Edit'))
Regarding @Damian's answer, osv.except_osv is marked in the source as deprecated but hasn't actually been deprecated yet as it is still in a lot of Odoo code. There is a newer openerp.exceptions.except_orm and the even newer Warning. Confusing to say the least.
Also, don't forget to translate. Using except_orm as an example.
from openerp.exceptions import except_orm
from openerp.tools.translate import _
raise osv.except_osv(_('Error!'), _("Something bad happened."))
or:
from openerp import _
from openerp.exceptions import Warning
raise Warning(_('Something bad happened.'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With