1
votes

I want to apply dynamic filter to a many2one field (F1) based on another field (F2). I've done that using @api.change decorator, and it works but not as expected.

When i create a new entity, i change the value of F2, then go to F1, i find it filered, Oki no problem.

When i close the form, and then edit it again, and i go directly to the F1 field, i get again all available possibilities (not filtered), i need first to go to F2 and then choose the same value (already chosen previously) and then come back to F1.

Any idea ? (below the code: F1 = product_id which is inherited, and F2 = bom_id)

class ProductionLot(models.Model):
    _inherit = "stock.production.lot"

    company_id = fields.Many2one(default=lambda self: self.env['res.company']._company_default_get('account.invoice'))

    bom_serial_number_line_ids = fields.One2many("mrp.bom.serialnumber.line", "parent_lot_id", "BoM Serial Numbers")
    bom_id = fields.Many2one("mrp.bom", "BoM")

    @api.onchange('product_id')
    def update_bom_id_from_product_id(self):
        for record in self:
            if (record.product_id):
                bom_complex_kit = record.product_id.env['mrp.bom']._bom_find(
                    product_tmpl=record.product_id.product_tmpl_id,
                    bom_type='complex_kit')

                self.bom_id = bom_complex_kit

            return {"domain": {"bom_id": [('product_tmpl_id.id', '=', record.product_id.product_tmpl_id.id),
                                          ('type', '=', 'complex_kit')]}}
1
do you have any update on this?pedrommuller

1 Answers

0
votes

As onchange filter will only be applied when the function is a trigger so it will only work when you change the on change value I guess what you need to do is this or combination of both onchange and default domain on field

def get_domain(self):
    ids = self.env['stock.production.lot'].browse(self._context.get('active_ids'))
    print("Here see all ids and use them accordingly",ids) 

bom_id = fields.Many2one("mrp.bom", "BoM", domain = lambda self:self.get_domain())