【问题标题】:Odoo: Conditional invisible attribute on fields only works in one direction?Odoo:字段上的条件不可见属性仅在一个方向上起作用?
【发布时间】:2023-03-30 09:27:01
【问题描述】:

我正在尝试在 Odoo 表单视图中使字段不可见。 当“可以出售”被选中时 ==>“产品经理”应该是不可见的:

我尝试在产品表单的继承视图中将属性“不可见”与域一起使用:

<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>

当sales_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>

为什么我不能对这个字段做同样的事情?

【问题讨论】:

    标签: xml inheritance odoo odoo-view


    【解决方案1】:

    这对我很有效

    <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 属性。如果原始表单中已经存在其他属性,我的解决方案可能会修改或删除它们。

    【讨论】:

    • 完美运行!故障在位置=“属性”。应该像你一样被“替换”。
    • 还有一个问题。这适用于 product_manager,但是当我用 loc_rack 尝试这个时,它不起作用?我已经编辑了我的 OP。
    • 确保您继承了正确的视图。 inherit_id 字段中有什么?
    • loc_rackstock.view_template_property_form 视图中
    【解决方案2】:

    使用position="replace"可能会带来问题,最好的选择是使用position="attributes"

    假设另一个已安装的模块(名为模块 X)正在继承您要替换的标签。当你更新你的 Odoo 系统时,它会因为模块 X 找不到你替换的标签而崩溃。

    这段代码非常适合我:

    <field name="product_manager"  position="attributes">
        <attribute name="attrs">{'invisible': [('sale_ok', '=', True)]}</attribute>
    </field>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多