Odoo 在 xml 中按州隐藏字段

Odoo hide field by state in xml

class myModel(models.Model):

    _name = 'my.model'

    state = fields.Selection(selection=_STATES, string='Status', index=True, track_visibility='onchange',required=True, copy=False, default='draft')
    my_model_line = fields.One2many('my.model.line', 'model_id')

另一个模型:

class MyModelLine(models.Model):

    _name = 'my.model.line'

    name = fields.Char(string='Name')
    quantity = fields.Integer(string='Quantity', required=True, default=1)
    remarks = fields.Text(string='Description')
    my_model_id = fields.Many2one('my.model', 'My Model')

my_model.xml

                <field name="my_model_line" attrs="{'readonly': [('state','not in', ('draft'))]}">
                    <tree string="My model Lines" editable="bottom">
                        <field name="name"/>
                        <field name="quantity"/>
                        <field name="remarks"/>
                        <button name="open_new_view" type="object" string="Add" class="oe_highlight"/>
                        <button icon="terp-face-plain" name="test" type="object" string="Add" class="oe_highlight"/>
                    </tree>
                 </field>

我要隐藏my.model.linemoel备注栏添加按钮栏 取决于 my.model 状态 。 例如,如果我的模型状态已获批准,则隐藏字段。

据我了解,我无法在我的 my_model.xml 中使用带域的不可见属性,因为 my.model.line 没有状态字段。也许有任何解决方案可以做到这一点?

我想在 my.model.line 对象中创建状态字段,蚂蚁根据 my.model 状态更改它的状态。

我也是这样尝试的:

<field name="remarks" attrs="{'invisible': "[('my_model_id .state', '=', 'approved')]"}"/>

但是我得到一个错误:"Unknown field my_model_id.state in domain"

首先要在 attrs 中使用字段,这个字段应该添加到表单视图中,它不仅是模型,请记住这一点,即使这意味着添加这个字段并使其不可见。

其次,如果你想显示具有 many2one 字段的其他模块的字段,请使用相关字段。

         state = fields.Selection( 'put same selzction here too',  related='many2one_field.state's, readonly=True) 

相关字段必须具有相同的类型如果字段是char使用char,如果字段是integer使用integer,如果字段是many2one使用many2one。 对于大多数字段,仅相关属性就足够了,但选择会重新定义相同的选择。

现在您可以像使用当前模型的字段一样使用该字段。

对不起我的英语,希望你能理解