Search code examples
pythonpython-2.7onchangeodoo-8odoo

If I change a field value in an api.onchange method, it's not updated in the view. Why?


I have a function executing in a api.onchange('bom_line_ids'), inside this function I want to have a if statement to clear bom_line_ids if a condition is matched. I used self.bom_line_ids.product_id = False

but for some reason the field is cleared (saw this in the prints, but it's not updated in the view).

@api.onchange('bom_line_ids')
def get_excl_list(self):

     list1 = []
     for i in self.excl_list:
         list1.append(i.id)

     list2 = []
     for j in self.bom_line_ids:
         list2.append(j.product_id.id)

         if j.product_id.id in list1:
             warning_message = "Product: " + self.env['product.template'].search([('id','=',j.product_id.id)]).name + " can't be added to the BoM.\n Please select another product."
             print "self.bom_line_ids.product_id", self.bom_line_ids.product_id
             self.bom_line_ids.product_id = False
             print "self.bom_line_ids.product_id", self.bom_line_ids.product_id

              return { 'warning': {'title': 'Product error', 'message':warning_message} }

Solution

  • There is an issue in Odoo talking about this. Someone already made a pull request.

    You can check as well this other question to solve the issue manually: One2many field on_change function can't change its own value?