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

7
当我点击按钮发送邀请后,会弹出一个成功发送邀请的提示框。但是问题在于,提示框的主标题为“Odoo服务器错误”,这是因为我使用了

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

有没有任何替代方案可以使其更好。


@adamStrauss,您能解释一下为什么您接受了我的答案,然后又将其删除吗?是答案有误还是您想要一种更高级的方法来实现它? - Charif DZ
1
@CharifDZ 对不起,那是我的错。你的回答是正确的。这是我的女儿调皮捣蛋惹出来的。无论如何,谢谢。 - Adam Strauss
3个回答

10

当我需要类似这样的东西时,我会使用一个带有“向导”和“消息”字段的虚拟表单,并创建一个简单的表单视图来显示该字段的值。

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

     @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'
        }

消息向导的表单视图就像这样简单:

<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中运行,如果有任何exceptionraised,Odoo将在database中执行rollback,如果您没有在此之前手动commit您的工作,您将会失去数据,这也不是在Odoo中推荐的方式。


如果我们在Odoo 15中使用ir.actions.server,这个解决方案就不起作用了。 - fudu
这个解决方案在Odoo 14中非常有效 :) - NM Naufaldo

3
如果之前的回答不起作用,请尝试以下方法:
在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>

希望这能有所帮助,谢谢。

-1
如果您有一个按钮且文本是静态的,您也可以在XML中使用button confirm属性,例如:
 <button string="Change Next Activity Date" name="change_next_activity_date"
                                type="object" class="btn-primary" confirm="are you sure you want to change the Date?"/>
                        

它将显示一个带有“是”和“取消”按钮的确认对话框,并且不会中断进程。与“是”或“否”选项相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接