0
votes

Im new to odoo. Im using odoo 13 and I have a many2one field and a many2many field with domain on the many2one field. What I need is that if the user changes the many2one field the many2many gets empty and ready to choose. but I don't know how to code that on a function so I have tried some variants but it just dont come to me. If I use this I just get an error when Im going to select for the first time the 'industria' field

class Partner(models.Model):
    _inherit = 'res.partner'
    industria = fields.Many2one('industria_model', string="Industria")
    marca = fields.Many2many('marca_model',
                             domain="[('industria_id', '=', industria)]", string="Marca")

    @api.onchange('industria')
    def get_price(self):
        self.marca = [(5)]
        # if self.marca:
        #     self.marca = [(5, 0, 0)]
2
marca = False, marca = [(6, 0, [])], marca = [] I think all of this works as I rememberCharif DZ
The error message or traceback would have been nice ;-)CZoellner

2 Answers

0
votes

[(5)] will be evaluated to [5] then Odoo will try to return the corresponding record that is not created yet (at the first time) and you should see the following error:

Record does not exist or has been deleted. (Record: marca_model(5,), 

You can simply use [(5,)] to empty the marca field.

If a record with id 5 exists in database, you will get the same result as using [(4, 5)]

0
votes

In the end I used this

@api.onchange('industria')
    def change_brand(self):
        self.marca = [(6, 0, [])]

I had already tested it but I had to restart odoo to see the changes, just updating the module didn't worked