如何 return 一条消息并同时更新值(从按钮)

how to return a message and update values at the same time (from button)

我有一个带有按钮的表单视图,其 objective 用于测试连接。 如果连接成功,我需要 return 一条消息,同时更新表单视图中的一些值。

我的 .py 文件中有这样的东西:

def test_connection(self):        
        connected = self.connect(self.ip_address, self.port)
        if connected:
            self.data1='a'
            self.data2='b' 
        return True

到目前为止,我只能更新值,但如果我发出警告消息:raise Warning(_('Connection OK.')),则数据不会更新。

有什么方法可以在更新表单数据的同时显示信息?或者还有其他方法可以完成这样的事情吗?

无需使用 raise 消息,您只需在向导中定义一个 message 字段即可。

message = fields.Char('Message')    

def test_connection(self):
    connected = self.connect(self.ip_address, self.port)
    if connected:
        self.update({data1: 'a',
                     data2: 'b',
                     message: 'Your Message'})
    return True

除非有消息集,否则您可以在视图上以这样的方式显示它invisible

<field name="message" attrs="{'invisible': [('message', '=', False)]}"/>

Views documentation

您不必定义消息字段,只需添加具有漂亮 css 样式的 div 标签即可。并根据连接状态使用attrs来显示或隐藏它。

在 odoo 中你不能同时显示错误和更新值

您可以同时更新值和 return 向导作为消息。我认为这很好。如果使用 ant exceptions 或 warning raised,服务器流将阻塞在那里,因此无法更新值,同时更新的值将被撤销。