2015-10-13 101 views
3

我如何檢查添加到mrp.bom.line(Many2many)的新行的product_id是否等於固定的id('= 17'),它可能永遠不會被添加到BoM。但我似乎無法做到這一點,他將新行存儲爲「NewId」,他只在按下SAVE按鈕時將其寫入數據庫。我知道NewId是保存在緩存中的某種ID。當它等於17時,我怎麼能刪除這條線?Odoo 8 Many2many驗證並刪除行

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

回答

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