Skip to main content
 首页 » 编程设计

python之如果我在 api.onchange 方法中更改字段值,它不会在 View 中更新。为什么

2025年05月04日15bjzhanghao

我有一个函数在 api.onchange('bom_line_ids') 中执行,在这个函数中我想有一个 if 语句来清除 bom_line_ids 如果条件匹配。我使用了 self.bom_line_ids.product_id = False

但由于某种原因,该字段已被清除(在打印件中看到了这一点,但未在 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} } 

请您参考如下方法:

Odoo 中有一个问题正在讨论这个问题。 Someone already made a pull request .

您也可以检查其他问题以手动解决问题:One2many field on_change function can't change its own value?