打开表单时需要 odoo 中的表单警告字段

问题描述 投票:0回答:1

我正在尝试添加警告字段以形成表单,如果字段中有消息,则每次打开表单时都会弹出该消息:

message_warning = fields.Char(string="Warning", )

如果它是空的,什么也没发生,并且用户添加文本,那么每次表单打开表单弹出窗口中的消息时,我都想要它。 我在网上搜索,发现这个函数应该可以完成任务,但什么也没发生,没有弹出:

@api.model    
def form_view_get(self, access_uid, view_id=None, view_type='form', context=None, options=None):
    res = super(Model_name, self).form_view_get(access_uid, view_id, view_type, context, options)        
    if context.get('create') or context.get('edit'):  # Check if opening for create or edit            
        record_id = context.get('id')  # Get record ID if available            
    if record_id and self.env['model.name'].browse(record_id).required_field:                # Raise warning without preventing form opening                
        raise UserError("message!")

我确实尝试了上述操作,打开表单时没有出现任何消息,也没有错误消息,只是什么也没发生。

python-3.x odoo
1个回答
0
投票

您可以使用表单视图顶部的 div 来显示警告消息

示例:

<div class="alert alert-warning" role="alert" invisible="not message_warning">
    <field name="message_warning" readonly="1"/>
</div>

您还可以制作自定义表单视图

示例:(覆盖表单渲染以在打开表单视图时显示警告消息)

/** @odoo-module **/

import { formView } from "@web/views/form/form_view";
import { FormRenderer} from "@web/views/form/form_renderer";

import { WarningDialog } from "@web/core/errors/error_dialogs";
import { registry } from "@web/core/registry";
const viewRegistry = registry.category("views");
import { useService } from "@web/core/utils/hooks";

export class CustomFormRenderer extends FormRenderer {
    setup() {
        var self = this;
        super.setup();
        const message = self.props.record.data.message_warning;
        if (message) {
            useService("dialog").add(WarningDialog, {
                 title: "Warning",
                 message: message
             });
        }
    }
};

export const CustomFormView = {
     ...formView,
     Renderer: CustomFormRenderer
 };

viewRegistry.add('custom_form_view', CustomFormView);

将以下内容添加到

__manifest__.py

"assets": {
    'web.assets_backend': [
        'form_warning/static/src/js/form_view.js',
    ],
},

要设置自定义表单视图,请使用表单视图的

js_class
属性。

注:
您需要扩展上面的代码,以在用户单击寻呼机显示表单视图时显示警告

© www.soinside.com 2019 - 2024. All rights reserved.