当我点击按钮发送邀请后,会弹出一个成功发送邀请的提示框。但是问题在于,提示框的主标题为“Odoo服务器错误”,这是因为我使用了 标签。
raise osv.except_osv("Success", "Invitation is successfully sent")
有没有任何替代方案可以使其更好。
raise osv.except_osv("Success", "Invitation is successfully sent")
有没有任何替代方案可以使其更好。
当我需要类似这样的东西时,我会使用一个带有“向导”和“消息”字段的虚拟表单,并创建一个简单的表单视图来显示该字段的值。
每当我想在点击按钮后显示消息时,我就会这样做:
@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
中运行,如果有任何exception
被raised,Odoo将在database
中执行rollback
,如果您没有在此之前手动commit
您的工作,您将会失去数据,这也不是在Odoo中推荐的方式。
ir.actions.server
,这个解决方案就不起作用了。 - fudu# 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 _
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>
<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?"/>
它将显示一个带有“是”和“取消”按钮的确认对话框,并且不会中断进程。与“是”或“否”选项相同。