Reputation: 489
I am trying to override a python method from Odoo but getting no success in that.
for that what I am doing is
from odoo import models, fields, api
class MYBaseModel(models.BaseModel):
_register = False
@api.multi
def unlink(self):
print "My Method called"
return super(MYBaseModel, self).unlink()
What I wanted to achieve is when the unlink method is executed by the odoo framework, my method should be called,
but right now my method is not getting called, and I don't know the reason. can anyone show me what I am doing wrong ?
EDIT :
My Goal is to call Unlink method for all the models. so in any model the record is deleted my method should be called and then the base method should be called.
Upvotes: 1
Views: 5122
Reputation: 56
You need to add the _inherit
property like this:
from odoo import models, fields, api
class MYBaseModel(models.BaseModel):
_register = False
_inherit = 'my.base.model'
@api.multi
def unlink(self):
print "My Method called"
return super(MYBaseModel, self).unlink()
EDIT:
import odoo.models.BaseModel as base
class newClass(base):
def unlink(self):
# your code
return super(newClass, self).unlink()
Upvotes: 1
Reputation: 805
Try this _register_hook
method
For more detail and example check the file from Addons.
./addons/base_action_rule/models/base_action_rule.py
Upvotes: 2
Reputation: 342
try using the following Code:
from odoo import models, fields, api
class MYBaseModel(models.Model):
_register = False
_inherit = "ir.model"
@api.multi
def unlink(self):
print "My Method called"
return super(MYBaseModel, self).unlink()
Upvotes: 0
Reputation: 98
As Surajano said in its answer,
What you did for now is defining a new BaseModel. But for now, no any magic will make it works on existing models.
You have 2 options (at least):
1- override existing models by python-inheriting them with MyBaseModel
: (Surajano suggestion)
from odoo.addons.your_addon.models import MYBaseModel
class AccountInvoice(MYBaseModel):
_inherit = 'account.invoice'
# now, invoices (and only them) will benefit or your override
2- Otherwise, you could do a monkey-patch :
odoo.models.BaseModel.unlink = MYBaseModel.unlink
# or even this : odoo.models.BaseModel = MYBaseModel, but i'm not really sure it will work
It will be available for every Odoo models, but this approach is bit more "definitive"
(EDIT: i'm not sure, but you may need to keep a trace of the original unlink method before monkey-patching it, and using this original_method in your unlink() override)
Hope it helps you,
Upvotes: 0
Reputation: 2688
I think you have written the function correct, but miss to add _inherit
in your class.
Here is what you need to do, you need to add _inherit='object.name' in your Class MYBaseModel
.
instead of super(MYBaseModel, self).unlink()
call models.BaseModel.unlink()
but that will skip all unlink() extensions for your model.
Upvotes: 2