[英]Odoo: Conditional invisible attribute on fields only works in one direction?
我正在嘗試在 Odoo 表單視圖中根據條件使字段不可見。 檢查“可以銷售”==>“產品經理”應該是不可見的:
我嘗試在產品表單的繼承視圖中使用屬性“invisible”和域:
<record model="ir.ui.view" id="product_template_form_inherit">
<field name="name">product.template.product.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view" />
<field name="arch" type="xml">
<field name="product_manager" position="attributes">
<attribute name="invisible">[('sale_ok', '=', True)]</attribute>
</field>
</field>
</record>
當字段 sale_ok 為真時, product_manager 字段實際上是隱藏的。 但是當字段 sale_ok 再次變為 false 時,字段 product_manager 保持隱藏狀態。
我也試過這個:
<field name="product_manager" attrs="{'invisible': [('sale_ok', '=', True)]}"/>
這也行不通。
我還嘗試過其他域,例如:
[('sale_ok', '==', True)]
[('sale_ok', '!=', False)]
[('sale_ok', '=', 'True')]
不太確定這里出了什么問題......如何在(未)選中時使其(不)可見?
我最終追求的是以下內容:選中復選框時,表單應立即更改而不保存。 必須添加和刪除字段。 那可能嗎?
編輯:
我現在可以使用 ChesuCR 的答案隱藏/取消隱藏產品經理。 但是,當我用“loc_rack”(存儲位置==>機架)嘗試同樣的事情時,它給出了錯誤:
Field(s) `arch` failed against a constraint: Invalid view definition
Error details:
Element '<field name="loc_rack">' cannot be located in parent view
這是我使用的代碼:
<field name="loc_rack" position="replace">
<field name="loc_rack" attrs="{'invisible': [('sale_ok', '=', True)]}"/>
</field>
為什么我不能對這個領域做同樣的事情?
這對我很有效
<record id="custom_product_template_form_view" model="ir.ui.view">
<field name="name">custom.product.template.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<field name="product_manager" position="replace">
<field name="product_manager" attrs="{'invisible': [('sale_ok', '=', True)]}"/>
</field>
</field>
</record>
如果您發現任何問題,您可以嘗試使用“federico”答案來修改attrs
屬性。 如果其他屬性已經存在於原始表單中,我的解決方案可能會修改或刪除它們。
使用position="replace"
可能會帶來問題,最好的選擇是使用position="attributes"
想象另一個已安裝的模塊(名為模塊 X)正在繼承您要替換的標簽。 當您更新您的 Odoo 系統時,它會崩潰,因為模塊 X 找不到您替換的標簽。
這段代碼非常適合我:
<field name="product_manager" position="attributes">
<attribute name="attrs">{'invisible': [('sale_ok', '=', True)]}</attribute>
</field>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.