如何显示openerp错误信息

时间:2021-02-20 11:49:45

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.

在我的学生模块中,我继承了res.partner。如果合作伙伴是学生,我想限制在合作伙伴视图中修改名称。这是我的电话。

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?

它工作,但我想在消息框中显示错误消息。不例外。我怎么能这样做?

4 个解决方案

#1


6  

raise osv.except_osv(('Error'), ('Error  Cannot Edit'))

#2


5  

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.

关于@Damian的答案,osv.except_osv在源代码中被标记为已弃用但尚未被弃用,因为它仍然存在于很多Odoo代码中。有一个较新的openerp.exceptions.except_orm和更新的警告。很难说最少。

Also, don't forget to translate. Using except_orm as an example.

另外,不要忘记翻译。以except_orm为例。

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.'))

#3


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

看到这个链接:https://www.odoo.com/forum/how-to/developers-13/what-should-onchange-methods-do-and-return-57760

#4


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,
                    }

#1


6  

raise osv.except_osv(('Error'), ('Error  Cannot Edit'))

#2


5  

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.

关于@Damian的答案,osv.except_osv在源代码中被标记为已弃用但尚未被弃用,因为它仍然存在于很多Odoo代码中。有一个较新的openerp.exceptions.except_orm和更新的警告。很难说最少。

Also, don't forget to translate. Using except_orm as an example.

另外,不要忘记翻译。以except_orm为例。

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.'))

#3


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

看到这个链接:https://www.odoo.com/forum/how-to/developers-13/what-should-onchange-methods-do-and-return-57760

#4


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,
                    }