3

我想如何检查添加到 mrp.bom.line (Many2many) 的新行的 product_id 是否等于可能永远不会添加到 BoM 的固定 id ('=17')。但我似乎无法做到这一点,因为他将新行存储为“NewId”,他只在我按下 SAVE 按钮时将其写入数据库。我知道 NewId 是保存在缓存中的某种 ID。当它等于 17 时,如何删除该行?

@api.onchange('bom_line_ids')

def methodA(self):
     list = []
     fixed_list = [17]

     for i in self.bom_line_ids:
         list.append(i.product_id.id)

     for j in list:
        if j in fixed_list:

        HERE DELETE THIS MRP.BOM.LINE AND PRINT WARNING MESSAGE

        warning_message = "Product: " + self.env['product.template'].search([('id','=',j)]).name + " can't be added to the BoM.\n Please select another product."
            return { 'warning': {'title': 'Product error', 'message':warning_message} }
4

1 回答 1

0

如果你有固定的 id 来检查,那么这段代码会有很多帮助。并减少执行时间

@api.onchange('bom_line_ids')
def methodA(self):
    fixed_product_id = 17
    is_found = False
    for bom_line in self.bom_line_ids:
        if bom_line.product_id.id == fixed_product_id:
            is_found =True
            bom_line.unlink()

    if is_found:
        warning_message = "Product: " + self.env['product.template'].search([('id','=',fixed_product_id)]) + " can't be added to the BoM.\n Please select another product."
        return { 'warning': {'title': 'Product error', 'message':warning_message} }
于 2017-04-03T11:29:10.560 回答