根据odoo 8中的另一个2,在one2many中过滤many2one

时间:2021-03-30 20:23:28

my problem is following:

我的问题如下:

I have two one2many fields in my class Agreement which each contains a many2one field referring to the class Unit.

我的类协议中有两个one2many字段,每个字段包含一个涉及类Unit的many2one字段。

I want to be able to select some Units in agreement_invoice_ids and the Units in agreement_info_ids should automatically be filtered based one this selection.

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

I tried using a domain in the xml, setting up the domain in python, different functions using api.onchange and api.depends. I hope that it is actually possible and I just missed the correct thing to do.

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

I greatly take any piece of advise.

我非常重视任何建议。

Below a simplified version of my code:

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

agreement.py

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

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 个解决方案

#1


3  

I figured it out.

我想到了。

Just do the following and you're golden.

只要做以下事情,你就是金色的。

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

#1


3  

I figured it out.

我想到了。

Just do the following and you're golden.

只要做以下事情,你就是金色的。

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