简体   繁体   中英

Set the filter for one2many list showing based on the condition of many2one field? (Odoo 13)

Im trying to set the filter for one2many list showing based on the condition of many2one field.

model_a.py:

 name = fields.Many2one('hr.employee')
 keya = fields.Many2one('model.b')
 num_a = fields.Monetary()

model_b.py:

 name = fields.Many2one('hr.employee')
 o_2_m = fields.One2many('model.a', 'keya', string='one2many list will be filter base on num_c')
 value_c = fields.Many2one('model.c', string='Value of C', required= True)

model_c.py:

 _rec_name = "code_c"
 code_c = fields.Char('Code', required= True)
 num_c = fields.Monetary()

By default, the one2many list will show all records if the user will not select record of value_c.

If the user selects record via value_c field which has created contains the value of num_c, the one2many list will be filtered for showing base on the value of num_c.

For Example: if the num_a in one2many field is: 15

Then when the user fills in num_c field is: 15 too

Then the one2many list just shows all the records which have the value: 15

That's the key condition for the filter of one2many list.

I'd tried with these codes in model_b.py:

 @api.onchange('value_c')
 def onchange_value_c(self):
    for rec in self:
       if rec.value_c and rec.o_2_m:
          for line in rec.value_c:
             find_c = self.env["model.c"].search([('num_c', '=', line.num_c)])
             find_a = self.env["model.a"].search([('num_a', '=', line.num_a)])
             #compare value of num_c with num_a
             if find_c.num_c == find_a.num_a:
                 for abc in rec.o_2_m:
                    return {'domain': {'o_2_m': [('num_a','=', abc.find_a.id)]}}

But it still not works. It still shows all one2many list's records, after selecting the record of value_c and click on the "Add a line" link.

Please help!

Thank you!

Finally, I'd solved my requirement. Using onchange function in model_b.py like this:

@api.onchange('value_c') 
def onchange_get_value_c(self): 
  for rec in self: 
     if rec.value_c:
        for line in rec.value_c: 
            find_c = self.env["model.c"].search([('num_c', '=', line.num_c)])

            if find_c:
                 return {'domain': {'o_2_m': [('num_a','=', find_c.num_c)]}}

It works fine when the user selecting many2one 'value_c' field which has the value in field 'num_c', then when clicking on the "Add a line" link, it will show all records which have value in field 'num_a' match or equal to the value in field 'num_c'. If the user selecting many2one 'value_c' field but not set the value for field 'num_c' or the value does not match or equal to the value in field 'num_a'. Then when clicking on the "Add a line" link, it will show nothing, and raise inform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM