odoo12 修行提升篇之 自定义消息弹框 (三)

在odoo12开发过程中很可能会遇到部分页面需要进行消息提示的情况。

odoo12中可以使用raise抛出如UserError、Warning等异常进行消息提示,但是效果不友好。

上面有个Odoo Server Error提示,很不美观,并且在Dialog框中弹出提示时,存在订单行字段值丢失的情况。

方案:

1、创建my_message_wizard.py文件,并在__init__.py中添加。

2、my_message_wizard.py的内容如下:

class MyMessageWizard(models.TransientModel):
    _name = 'my.message.wizard'

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

    @api.multi
    def action_confirm(self):
        return {'type': 'ir.actions.act_window_close'}

3、在view文件夹下创建my_message_wizard.xml文件,并在__manifest__.py中引入。

<odoo>
    <record id="my_message_wizard_form" model="ir.ui.view">
        <field name="name">my.message.wizard.form</field>
        <field name="model">my.message.wizard</field>
        <field name="arch" type="xml">
            <form>
                <p>
                    <field name="message" readonly="1"/>
                </p>
                <footer>
                    <button name="action_confirm"
                            string="确认"
                            type="object"
                            default_focus="1"
                            class="oe_highlight"/>
                </footer>
            </form>
        </field>
    </record>
</odoo>

4、使用:

if <condition>:
    message = self.env['my.message.wizard'].create({'message': 'message'})
    return {
        'name': '提示',
        'type': 'ir.actions.act_window',
        'view_mode': 'form',
        'res_model': 'my.message.wizard',
        'res_id': message.id,
        'target': 'new'
    }

5、效果:

以上就是自定义消息提示的过程。

温馨提示,记得升级对应的模块。

 

git地址:https://github.com/SamNicole1809/odoo12_my_pro

 

posted @ 2021-03-16 17:24  御简  阅读(698)  评论(1编辑  收藏  举报