在类型odoo

时间:2016-03-10 06:23:04

标签: python list listview drop-down-menu openerp

我们如何过滤树视图中已选择的值。换句话说,我想从树视图中过滤掉已经作为项添加的ID。

以此为例。

enter image description here

如果我添加了类型的项目,请说' A'那么这不能再添加了。

模型

class res_partner(models.Model):
    _inherit = 'res.partner'

    id_info = fields.One2many('identification.info', 'id_partner')


class Identification_Information(models.Model):
    _name='identification.info'

    identification_type = fields.Many2one('identification.type', string='Type')

xml文件

<record id="customer_id" model="ir.ui.view">
    <field name="name">Sample Partner</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="partner_sample.view_res_partner_form_sample_inherit"/>
    <field name="arch" type="xml">
        <field name="driver" position="replace">
            <field name="id_info" string="IDs(No Expiration)">
                <tree editable="bottom">
                    <field name="identification_type"/>
                    <field name="identification_number"/>
                </tree>
            </field>
        </field>
    </field>
</record>

1 个答案:

答案 0 :(得分:1)

首先,我不认为此解决方案适用于editable="bottom"

您需要添加新字段,该字段将排除下拉列表的ID记录。例如,我只排除id = 1和2的项目。

class Identification_Information(models.Model):
    _name='identification.info'

    identification_type = fields.Many2one('identification.type', string='Type')

    excluded_types = fields.One2Many(
        'identification.type',
        string='Excluded types',
        # here you need create custom logic or call other python method
        default=lambda self: [1, 2],
        # field not store in db
        store=False,
    )

你的树:

<tree>
    <!-- hide field with excluded ids. This field is need because we use him in domain below -->
    <field name="excluded_types" invisible="1"/>
    <!-- set domain with ids from our custom field  -->
    <field name="identification_type" domain="[('id', 'not in', excluded_types),]"/>
    <field name="identification_number"/>
</tree>

如果你有identification.type的几个观点,这个解决方案很好。在一个,你需要显示所有项目,但第二个不是全部。

如果您需要限制所有视图的下拉列表。只需在域上为您的模型添加域:

class Identification_Information(models.Model):
    _name='identification.info'

    identification_type = fields.Many2one('identification.type', string='Type', domain=[('id', 'not in', [1, 2])])

希望这会对你有所帮助。