根据odoo 8

时间:2017-03-17 10:22:57

标签: python-2.7 one-to-many odoo-8 many-to-one

我的问题如下:

我的class Agreement中有两个one2many字段,每个字段都包含一个引用class Unit的many2one字段。

我希望能够在agreement_invoice_ids中选择一些单位,agreement_info_ids中的单位应根据此选项自动过滤。

我尝试在xml中使用域,在python中设置域,使用api.onchange和api.depends设置不同的函数。我希望它实际上是可能的,我只是错过了正确的事情。

我非常重视任何建议。

在我的代码的简化版本下面:

agreement.py

class Agreement(models.Model):
    _name = 'model.agreement'

    agreement_invoice_ids = fields.One2many('model.invoice', 'agreement_id')
    agreement_info_ids = fields.One2many('model.info', 'agreement_id')


class Invoice(models.Model):
    _name = 'model.invoice'

    agreement_id = fields.Many2one('model.agreement')
    unit_id = fields.Many2one('model.unit')


class Info(models.Model):
    _name = 'model.info'

    agreement_id = fields.Many2one('model.agreement')
    unit_id = fields.Many2one('model.unit')

agreement.xml

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="agreement_form_view">
            <field name="name">agreement.form</field>
            <field name="model">model.agreement</field>
            <field name="arch" type="xml">
                <form string="Agreement Form">

                    <field name="agreement_invoice_ids">
                        <tree editable="bottom">
                            <field name="unit_id"/>
                        </tree>
                    </field>

                    <field name="agreement_info_ids">
                        <tree editable="bottom">
                            <field name="unit_id"/> <!-- should be filtered based on selection made for unit_id in agreement_invoice_ids -->
                        </tree>
                    </field>

                </form>
            </field>
        </record>
    </data>
</openerp>

1 个答案:

答案 0 :(得分:3)

我明白了。

只需执行以下操作即可获得金奖。

class Info(models.Model):
    _name = 'model.info'

    agreement_id = fields.Many2one('model.agreement')
    unit_id = fields.Many2one('model.unit')

    @api.multi
    @api.onchange('unit_id')
    def filter_unit_id(self):
        res = dict()
        unit_ids = self.agreement_id.agreement_invoice_ids.mapped('unit_id.id')
        res['domain'] = {'unit_id': [('id', 'in', unit_ids)]}
        return res