如何在odoo中弹出成功消息?

How to popup success message in odoo?

我在点击按钮并成功发送邀请后通过点击按钮发送邀请有成功发送邀请的弹出消息。但问题是弹出消息的主标题是Odoo Server Error。那是因为我正在使用

raise osv.except_osv("Success", "Invitation is successfully sent")

有没有更好的办法

当我需要这样的东西时,我有一个带有 message 字段的虚拟 wizard,并且有一个显示该字段值的简单表单视图。

每当我想在点击按钮后显示一条消息时,我会这样做:

     @api.multi
     def action_of_button(self):
        # do what ever login like in your case send an invitation
        ...
        ...
        # don't forget to add translation support to your message _()
        message_id = self.env['message.wizard'].create({'message': _("Invitation is successfully sent")})
        return {
            'name': _('Successfull'),
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'res_model': 'message.wizard',
            # pass the id
            'res_id': message_id.id,
            'target': 'new'
        }

留言精灵form view就这么简单:

<record id="message_wizard_form" model="ir.ui.view">
    <field name="name">message.wizard.form</field>
    <field name="model">message.wizard</field>
    <field name="arch" type="xml">
        <form >
            <p class="text-center">
                <field name="message"/>
            </p>
        <footer>
            <button name="action_ok" string="Ok" type="object" default_focus="1" class="oe_highlight"/> 
        </footer>
        <form>
    </field>
</record>

Wizard 很简单就是这样:

class MessageWizard(model.TransientModel):
    _name = 'message.wizard'

    message = fields.Text('Message', required=True)

    @api.multi
    def action_ok(self):
        """ close wizard"""
        return {'type': 'ir.actions.act_window_close'}

注意:永远不要使用exceptions来显示信息消息因为所有运行都在一个大transaction 当你点击按钮时,如果 exception raised Odoo 会在 database 中执行 rollback,如果你不这样做,你将丢失数据 commit 在此之前先手动完成工作, 在 Odoo 中也不 推荐

如果之前的答案无效,试试这个:

在odoo 15版本中,你可以这样使用:

# show success message
title = _("Successfully!")
message = _("Your Action Run Successfully!")
return {
    'type': 'ir.actions.client',
    'tag': 'display_notification',
    'params': {
        'title': title,
        'message': message,
        'sticky': False,
    }
}

注:

记得在您的 python 文件中添加这一行来翻译消息:

from odoo import _

此外,您需要在 xml 文件中添加 action = ... 否则它将不起作用:

<field name="code">action = model.your_function()</field>

示例 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="action_your_action_name" model="ir.actions.server">
        <field name="name">Your Action</field>
        <field name="model_id" ref="model_your_model_name"/>
        <field name="binding_model_id" ref="mymodule.model_your_model_name"/>
        <!-- set this action is appear in form or list -->
        <field name="binding_view_types">list</field>
        <field name="state">code</field>
        <!-- function called -->
        <field name="code">action = model.your_function()</field>
    </record>
</odoo>

我不确定为什么,但是如果我们删除 action = ... 部分,那么它看起来像这样,它将不起作用:

<field name="code">model.your_function()</field>

希望对您有所帮助,谢谢。