2017-03-17 117 views
1

我的問題如下:過濾many2one基於另一個many2one在odoo 8

我在class Agreement均含有一many2one場指的是class Unit 2個one2many領域。

我希望能夠選擇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> 

回答

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 
+0

尼斯@gve,好工作,答問 –

+0

@gve完美的答案,謝謝:) –

相關問題