在 onchange 方法中显示消息并为字段赋值

Show a message and assign value to a field in an onchange method

我正在尝试编写一个 returns 消息并同时更新值的 onchange。到目前为止,它显示了消息,但字段保持不变。我的代码是:

@api.onchange('changed_field')
def my_onchange_method(self):    
    if self.other_field:
        self.changed_field=False
        raise Warning('Some message.')

我认为我的错误在于发送消息的方式,谁能告诉我如何在 odoo 9 中实现这一点?谢谢。

我认为您引发了 builtin 警告异常,这可能是该字段未更新的原因(我认为引发异常时更改会回滚) .

试试这个:

@api.onchange('changed_field')
def my_onchange_method(self):    
    if self.other_field:
        self.changed_field = False
        return {
            'warning': {
                'title': 'TITLE OF THE WARNING MESSAGE BOX',
                'message': 'YOUR WARNING MESSAGE',
            }
        }

我可以确认这至少适用于 odoo 8。它可能适用于 odoo 9。

def onchange_amount_paid(self, cr, uid, ids, amount_paid, context=None):


res = {'value':{}}

if amount_paid:

if fee_type==1 and amount_paid<70:

warning = { 'title': ("Warning"), 'message': ('registration account minimum payment is 70'), }

return {'value': res.get('value',{}), 'warning':warning} 

return {'value': res.get('value',{})}