简体   繁体   English

Python方法未覆盖

[英]Python Method is not overriding

I am trying to override a python method from Odoo but getting no success in that. 我正在尝试从Odoo覆盖python方法,但在此方面没有成功。

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, 我想要实现的是,当odoo框架执行unlink方法时,应调用我的方法,

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. 我的目标是为所有模型调用Unlink方法。 so in any model the record is deleted my method should be called and then the base method should be called. 因此,在任何模型中,删除记录都应先调用我的方法,然后再调用基本方法。

I think you have written the function correct, but miss to add _inherit in your class. 我认为您已正确编写了该函数,但是错过了在类中添加_inherit的功能。 Here is what you need to do, you need to add _inherit='object.name' in your Class MYBaseModel . 这是您需要做的,您需要在MYBaseModel Class MYBaseModel添加_inherit ='object.name'

instead of super(MYBaseModel, self).unlink() call models.BaseModel.unlink() but that will skip all unlink() extensions for your model. 而不是super(MYBaseModel, self).unlink()调用models.BaseModel.unlink()但这将跳过模型的所有unlink()扩展。

Try this _register_hook method For more detail and example check the file from Addons. 尝试使用_register_hook方法。有关更多详细信息和示例,请检查Addons中的文件。

./addons/base_action_rule/models/base_action_rule.py ./addons/base_action_rule/models/base_action_rule.py

You need to add the _inherit property like this: 您需要像这样添加_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()

EDIT: 编辑:

import odoo.models.BaseModel as base
class newClass(base):
    def unlink(self):
        # your code
        return super(newClass, self).unlink()

As Surajano said in its answer, 正如Surajano在回答中所说,

What you did for now is defining a new BaseModel. 您现在所做的是定义一个新的BaseModel。 But for now, no any magic will make it works on existing models. 但是目前,还没有任何魔术可以使它在现有模型上运行。

You have 2 options (at least): 您有2个选项(至少):

1- override existing models by python-inheriting them with MyBaseModel : (Surajano suggestion) 1-通过使用MyBaseModel进行python继承来覆盖现有模型:(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- Otherwise, you could do a monkey-patch : 2-否则,您可以做一个猴子补丁:

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" 每个Odoo型号都可以使用,但是这种方法更具“确定性”

( 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) 编辑 :我不确定,但是您可能需要先跟踪原始unlink方法,然后再进行猴子补丁,并在unlink()中使用此original_method重写)

Hope it helps you, 希望对您有帮助,

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM