6

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?

Chamal
  • 235
  • 1
  • 7
  • 13

4 Answers4

6
raise osv.except_osv(('Error'), ('Error  Cannot Edit'))
Damian
  • 449
  • 4
  • 11
6

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.'))
Adrian Merrall
  • 2,509
  • 12
  • 9
  • Thanks Adrian. It is working But I have a problem now... I just want to prohibit editing name in partner view. But now I cant even change the name from student view.. How could I do this? – Chamal Mar 17 '15 at 06:43
  • As of Odoo 10, except_orm is deprecated, and so is Warning. Check out this file, it is well commented: odoo/exceptions.py – Jerther May 03 '17 at 15:24
2

you can even combine the warning with an alternative value to set in the inputfield, like so:

   if not start_date < end_date:
       return {
           'warning': {
               'title': _('Warning'),
               'message': _('End date must be bigger than start date'),
           },
           'value': {
               'elec_end_agreement':
                   self.get_end_date(cr, uid, pp_id, start_date)
           },
       }

see this link: https://www.odoo.com/forum/how-to/developers-13/what-should-onchange-methods-do-and-return-57760

  • Just a note: This will only work in a onchange method. If you have to show a warning/error message in another function use the other answers. – ChristophLSA Apr 25 '16 at 08:53
0

Check the return section, that'll probably help you.

def check_partners_email(self, cr, uid, partner_ids, context=None):
        """ Verify that selected partner_ids have an email_address defined.
            Otherwise throw a warning. """
        partner_wo_email_lst = []
        for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids, context=context):
            if not partner.email:
                partner_wo_email_lst.append(partner)
        if not partner_wo_email_lst:
            return {}
        warning_msg = _('The following contacts have no email address :')
        for partner in partner_wo_email_lst:
            warning_msg += '\n- %s' % (partner.name)
        return {'warning': {
                    'title': _('Email addresses not found'),
                    'message': warning_msg,
                    }
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Capriatto
  • 947
  • 1
  • 8
  • 17