Python 方法未覆盖
Python Method is not overriding
我正在尝试覆盖 Odoo 的 python 方法,但没有成功。
为此我正在做的是
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()
我想实现的是当odoo框架执行unlink方法时,应该调用我的方法,
但是现在我的方法没有被调用,我不知道原因。谁能告诉我我做错了什么?
编辑:
我的目标是为所有模型调用 Unlink 方法。所以在任何模型中记录被删除我的方法应该被调用然后基本方法应该被调用。
我认为你写的函数是正确的,但是在你的 class 中没有添加 _inherit
。
这是您需要做的,您需要在 Class MYBaseModel
.
中添加 _inherit='object.name'
而不是 super(MYBaseModel, self).unlink()
调用 models.BaseModel.unlink()
但这将跳过您模型的所有 unlink() 扩展。
您需要像这样添加 _inherit
属性:
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()
编辑:
import odoo.models.BaseModel as base
class newClass(base):
def unlink(self):
# your code
return super(newClass, self).unlink()
正如 Surajano 在其回答中所说,
您现在所做的是定义一个新的 BaseModel。但就目前而言,没有任何魔法可以让它在现有模型上运行。
您有 2 个选项(至少):
1- 通过 python 覆盖现有模型 - 用 MyBaseModel
继承它们:(Surajano 建议)
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- 否则,你可以做一个猴子补丁:
odoo.models.BaseModel.unlink = MYBaseModel.unlink
# or even this : odoo.models.BaseModel = MYBaseModel, but i'm not really sure it will work
它将适用于每个 Odoo 模型,但这种方法有点多 "definitive"
(编辑:我不确定,但您可能需要在猴子修补之前保留原始取消链接方法的踪迹,并使用此 original_method 在你的 unlink() 覆盖中)
希望对你有帮助,
尝试使用以下代码:
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()
试试这个 _register_hook
方法
有关更多详细信息和示例,请查看 Addons 中的文件。
./addons/base_action_rule/models/base_action_rule.py
我正在尝试覆盖 Odoo 的 python 方法,但没有成功。
为此我正在做的是
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()
我想实现的是当odoo框架执行unlink方法时,应该调用我的方法,
但是现在我的方法没有被调用,我不知道原因。谁能告诉我我做错了什么?
编辑:
我的目标是为所有模型调用 Unlink 方法。所以在任何模型中记录被删除我的方法应该被调用然后基本方法应该被调用。
我认为你写的函数是正确的,但是在你的 class 中没有添加 _inherit
。
这是您需要做的,您需要在 Class MYBaseModel
.
而不是 super(MYBaseModel, self).unlink()
调用 models.BaseModel.unlink()
但这将跳过您模型的所有 unlink() 扩展。
您需要像这样添加 _inherit
属性:
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()
编辑:
import odoo.models.BaseModel as base
class newClass(base):
def unlink(self):
# your code
return super(newClass, self).unlink()
正如 Surajano 在其回答中所说,
您现在所做的是定义一个新的 BaseModel。但就目前而言,没有任何魔法可以让它在现有模型上运行。
您有 2 个选项(至少):
1- 通过 python 覆盖现有模型 - 用 MyBaseModel
继承它们:(Surajano 建议)
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- 否则,你可以做一个猴子补丁:
odoo.models.BaseModel.unlink = MYBaseModel.unlink
# or even this : odoo.models.BaseModel = MYBaseModel, but i'm not really sure it will work
它将适用于每个 Odoo 模型,但这种方法有点多 "definitive"
(编辑:我不确定,但您可能需要在猴子修补之前保留原始取消链接方法的踪迹,并使用此 original_method 在你的 unlink() 覆盖中)
希望对你有帮助,
尝试使用以下代码:
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()
试试这个 _register_hook
方法
有关更多详细信息和示例,请查看 Addons 中的文件。
./addons/base_action_rule/models/base_action_rule.py