如何根据Odoo中的选择字段值在树视图中显示字段?

时间:2023-01-17 15:40:50

I have a scenario where a field has to be displayed based on value of the selection field.

我有一个场景,必须根据选择字段的值显示一个字段。

class mro_order(osv.osv):
_inherit="mro.order"

 _columns={
        'released_part':fields.one2many('mro.released', 'released_id', 'Released parts',),
        'rel_id':fields.many2one('mro.released', 'Parts'),
        'parts_id1': fields.many2one('product.product', 'Parts', required=True, domain = [('maintain_ok','=',True)],),

    }

class mro_order_parts_line(osv.osv):



_inherit="mro.order.parts.line"

    _columns={
        'sts': fields.selection([
        ('new', 'New'),
        ('release', 'released'),

        ], 'State', default='new', readonly= False, select=True),
    }

    def onchange_sts(self, cr, uid, ids, sts):

            value = {}
            if sts == 'release':
                    value['parts_id'] = fields.many2one('mro.released','Parts')
            return value


class mro_released(osv.Model):
    _name="mro.released"

    _columns={
        'name': fields.char('Description', size=64),
            'parts_id': fields.char('Parts'),
            'parts_qty': fields.float('Quantity'),
            'parts_uom': fields.many2one('product.uom','Unit of Measure'),
            'released_id': fields.many2one('mro.order', 'Maintenance Order'),
        'state': fields.selection([
        ('scrap', 'Scrap'),
        ('reusable', 'Re-usable'),

        ], 'State', readonly= False, select=True, required=True),

    }



This is my xml file

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="released_parts_id">
                <field name="name">mro.order.form</field>
                <field name="model">mro.order</field>
                <field name="inherit_id" ref="mro.mro_order_form_view"/>
                <field name="arch" type="xml">
            <xpath expr="//form/sheet/notebook/page/group/field/tree/field[@name='parts_id']" position="before">
                <field name="sts" onchange="onchange_sts(sts)"/>
            </xpath>

            <xpath expr="//form/sheet/notebook/page/group[@string='Planned Parts']" position="after">
                            <group string="Released Parts">
                                        <field name="released_part" nolabel="1">

                                            <tree string="Released Parts" editable="bottom">
                                                <field name="parts_id" />
                                                <field name="parts_qty"/>
                                                <field name="parts_uom" />
                                                <field name="name"/>
                                            </tree>


                                        </field>
                                </group>

                    </xpath>
                </field>
            </record>
    </data>
</openerp> 

Based on the value of selection field, I want to change the relation of a many2one field or based on the value of selection field replace another many2one field named 'A' with different relation to replace the other many2one field named 'B' with different relation in the tree view of xml.

基于选择字段的值,我想改变many2one字段的关系或者基于选择字段的值替换另一个具有不同关系的另一个名为'A'的字段,以用不同的关系替换另一个名为'B'的many2one字段在xml的树视图中。

Kindly anyone has any idea on this?

请问有谁对此有任何想法?

1 个解决方案

#1


0  

You can use attrs attribute for this.

您可以使用attrs属性。

attrs

ATTRS

dynamic attributes based on record values.

基于记录值的动态属性。

A mapping of attributes to domains, domains are evaluated in the context of the current row's record, if True the corresponding attribute is set on the cell.

在当前行的记录的上下文中评估属性到域,域的映射,如果为True,则在单元格上设置相应的属性。

Possible attributes are invisible (hides the button) and readonly (disables the button but still shows it)

可能的属性是不可见的(隐藏按钮)和只读(禁用按钮但仍显示它)

Here for this define two fields separately and give the correct attrs attribute to view the correct one.

这里为此分别定义两个字段并给出正确的attrs属性以查看正确的属性。

For Eg:- you can give attrs attribute like the following for the field you want to show when the selection field is "new"

对于例如: - 当选择字段为“新”时,您可以为要显示的字段提供如下所示的attrs属性

attrs="{'invisible':[('sts', '!=', 'new')]}"

From the comments:- code for your condtion

从评论: - 你的condtion代码

                <field name="parts_id1" attrs="{'invisible':[('sts', '!=', 'new')]}"/>
                <field name="parts_id2" attrs="{'invisible':[('sts', '!=', 'release')]}"/>

Hope this helps.

希望这可以帮助。

#1


0  

You can use attrs attribute for this.

您可以使用attrs属性。

attrs

ATTRS

dynamic attributes based on record values.

基于记录值的动态属性。

A mapping of attributes to domains, domains are evaluated in the context of the current row's record, if True the corresponding attribute is set on the cell.

在当前行的记录的上下文中评估属性到域,域的映射,如果为True,则在单元格上设置相应的属性。

Possible attributes are invisible (hides the button) and readonly (disables the button but still shows it)

可能的属性是不可见的(隐藏按钮)和只读(禁用按钮但仍显示它)

Here for this define two fields separately and give the correct attrs attribute to view the correct one.

这里为此分别定义两个字段并给出正确的attrs属性以查看正确的属性。

For Eg:- you can give attrs attribute like the following for the field you want to show when the selection field is "new"

对于例如: - 当选择字段为“新”时,您可以为要显示的字段提供如下所示的attrs属性

attrs="{'invisible':[('sts', '!=', 'new')]}"

From the comments:- code for your condtion

从评论: - 你的condtion代码

                <field name="parts_id1" attrs="{'invisible':[('sts', '!=', 'new')]}"/>
                <field name="parts_id2" attrs="{'invisible':[('sts', '!=', 'release')]}"/>

Hope this helps.

希望这可以帮助。