3
votes

I want to change the retrieved set of values of a many2many field according to a many2one field onchange function, it worked well with another many2one field, but doesn't seem to filter the outcome on a many2many field my code is as follows

class CustomPurchase(models.Model):
    _name = 'custom.purchase'
    _description = 'Purchase Record'

    supplier_id = fields.Many2one('custom.supplier', string='Supplier', required=True)
    product_ids = fields.Many2many('custom.supply.line', string='Purchase Lines', required=True)

    @api.onchange('supplier_id')
    def onchange_supplier(self):
        selected_lines = []
        if self.supplier_id:
            for rec in self:
                selected_lines = rec.env['custom.supply.line'].search([('supplier_id', '=', rec.supplier_id.id)])
                domain = {'product_ids': [('id', '=', selected_lines.id)]}
            return {'domain': domain, 'value': {'selected_lines': []}}

Expected behavior is to have just the items related to the supplier_id many2one field

Produced behavior is all the items are retrieved

Edit: I've noticed that i when i remove widget="section_and_note_one2many" the domain works perfectly, yet the tree view can't be edited within the same form, even with editable="bottom"

1

1 Answers

0
votes

You can add check using supplier field directly inside domain.

Code:

    @api.onchange('supplier_id')
    def onchange_supplier(self):
        domain = {'product_ids': []}
        for rec in self:
            if rec.supplier_id:
                domain = {'product_ids': [('supplier_id', '=', rec.supplier_id.id)]}
        return {'domain': domain}

UPDATE: Try to implement this as same as in /addons/mrp/models/mrp_bom.py on attribute_value_ids. [Version:12]