2

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} }
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Jesse
  • 727
  • 13
  • 44

3 Answers3

1

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?

Community
  • 1
  • 1
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
0

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
Quentin THEURET
  • 1,222
  • 6
  • 12
  • Hi, thank you for your answer! I tried what u said but the self.bom_line_ids[self.bom_line_ids.index(j)].product_id = False gave me the error that mrp.bom.line has no attribute Index. The j.product_id = False results for the product_id field in the view which stillremains the value I selected, when the print out still shows it's empty. When i Save the BoM the value of product_id is also stored in the db,.. – Jesse Oct 15 '15 at 14:13
0

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 
Arsalan Sherwani
  • 889
  • 2
  • 25
  • 60