Jesse
Jesse

Reputation: 769

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} }

Upvotes: 2

Views: 4149

Answers (3)

Arsalan Sherwani
Arsalan Sherwani

Reputation: 560

Yes it happens. When You try to set value for a field through onchange method, the change doesnot appear on the view. However the field value does change. I fixed this issue by setting the 'compute in the field property and the function set here is the same as in the on_change'. So the value get updated in the view as well. For eg ->

job_titles = fields.Char('Job',compute='myemply_name',store=True) 

    @api.onchange('new_empl')
    @api.depends('new_empl')
    def myemply_name(self):
        self.job_titles = value 

Upvotes: 0

ChesuCR
ChesuCR

Reputation: 9640

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?

Upvotes: 1

Quentin THEURET
Quentin THEURET

Reputation: 1232

Maybe you have to select on which record on bom_line_ids you want to remove the product, because self.bom_line_ids is a one2many.

Replace

self.bom_line_ids.product_id = False

by

j.product_id = False 

or

self.bom_line_ids[self.bom_line_ids.index(j)].product_id = False

Upvotes: 0

Related Questions