2016-08-24 35 views
0

在我的odoo應用程序中,我有兩個模型。一個主要模型和一個兒童模型。在odoo中刪除子模型(使用繼承主模型創建)的記錄時,如何刪除主模型的記錄

主力機型

class main_model(models.Model): 
    _name = 'main.model' 

我的孩子模型

class child_model(models.Model): 
    _inherits = { 
    'main.model: 'main_ref' 
     } 
    _name = 'child.model' 

    main_ref = fields.Many2one('main.model') 

有了這個編碼。當我在子模型中創建一條記錄時,還會在主模型中創建一條記錄。但是當我在子模型中刪除一條記錄時,主要對應的記錄也應該刪除。

對於這是我做的是...

@api.multi 
def unlink(self): 
    self.main_ref.unlink() 
    return super(child_model, self).unlink()  

但是,這是行不通的。它顯示誤差..在字段定義

The operation cannot be completed, probably due to the following:- deletion: you may be trying to delete a record while other records still reference it- creation/update: a mandatory field is not correctly set 

回答

1

使用屬性ondelete = '級聯'

main_ref = fields.Many2one('main.model', ondelete='cascade') 

希望它能解決您的問題。

+0

感謝它的工作。注意:(它可以同時使用ondelete和unlink方法) –