当记录具有特定状态时,如何使Odoo表单只读?



我有一个实体"payment"在奥都有好几个州。我想在只读模式下打开支付的形式(禁用编辑按钮),当支付有状态"已付"。我怎么做呢?

正确的方法是为所有字段添加readonly属性,如下所示:

<field name="my_field" attrs="{'readonly':[('state','=','paid')]}"/>

但是如果你仍然想隐藏编辑按钮,那么你需要重写表单控制器,方法如下:

  1. 在你的模块静态文件夹中创建一个javascript文件:my_module/静态/src/js/my_form_view.js
  2. 添加到my_form_view.js中:
odoo.define('my_module.MyCustomForm', function (require) {
"use strict";
var FormController = require('web.FormController');
var viewRegistry = require('web.view_registry');
var FormView = require('web.FormView');
var MyFormController = FormController.extend({
_updateButtons: function () {
this._super.apply(this, arguments);
if (this.$buttons) {
if (this.renderer.state.data.state !== 'paid'){
this.$buttons.find('.o_form_button_edit').show();
} else {
this.$buttons.find('.o_form_button_edit').hide();
}
}            
},
});
var MyFormView = FormView.extend({
config: _.extend({}, FormView.prototype.config, {
Controller: MyFormController,
}),
});
viewRegistry.add('custom_form', MyFormView);
});
  1. 将js脚本添加到资源后端,创建一个新文件my_module/views/asset.xml,其内容应为:
<?xml version="1.0"?>
<odoo>
<template id="assets_backend" name="my_backend_assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/my_module/static/src/js/my_form_view.js" />
</xpath>
</template>
</odoo>
  1. 你还需要添加自定义表单视图到表单视图,像这样
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="my_model_form_view" model="ir.ui.view">
<field name="name">my.model.form</field>
<field name="model">my.model</field>
<field name="arch" type="xml">
<form js_class="custom_form">
... your form contents
</form>
</field>
</record>

</odoo>

你需要创建一个新的表单,它将开始如下:

<!-- consider the xml id for this form view is small_module.my_custom_form -->
<form create="false" edit="false">
</form>

那么在你的动作中你将调用这样的形式

<record id="my_custom_action" model="ir.actions.act_window">
<field name="name">The Best Action</field>
<field name="res_model">custom.model.action</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="small_module.my_custom_form"/>
</record>

或者您可以在Python方法中返回这样的操作。

return {
'type': 'ir.actions.act_window',
'name': _('The Best Action'),
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'custom.model.action',
'view_id': self.env.ref('small_module.my_custom_form').id,
}

最新更新