简体   繁体   English

如何在继承 controller Odoo 12 中覆盖 function 仅在此模块中使用

[英]how to override function in inherit controller Odoo 12 only using in this module

My core controller class:我的核心 controller class:

class ReportController(http.Controller):
   @http.route('/report/download_document/<reportname>/<docids>', type='http', auth="user")
   @serialize_exception
   def download_document(self, **kw):

My inherit class:我继承 class:

from odoo.addons.my_module.controllers.main import ReportController as RC

class ReportControllerProject(RC):
# Override method: download_document in my_module
   @http.route('/report/download_document/<reportname>/<docids>', type='http', auth="user")
   @serialize_exception
   def download_document(self, **kw):

But when I use action to download_document in another module it still uses the function of the inherit class.但是当我在另一个模块中使用 action to download_document 时,它仍然使用继承 class 的 function。

I want this function to be used only in the inherit class of this module, not everywhere, so what am I gonna do?我希望这个 function 只用在这个模块的继承 class 中,而不是无处不在,那我该怎么办?

The other modules will use the function of the module they are dependant on.其他模块将使用它们所依赖的模块的 function。 So in the manifest of the module where you want to use the overridden function make sure to depend from my_module.因此,在要使用覆盖的 function 的模块清单中,请确保依赖于 my_module。

__manifest__.py
.
.
.
'depends': [
    'my_module',
],
.
.

To resolve this situation, I just use condition and call super() function to execute this overriden funtion为了解决这种情况,我只是使用条件并调用 super() function 来执行这个覆盖函数

from odoo.addons.my_module.controllers.main import ReportController as RC

class ReportControllerProject(RC):
    # Override method: download_document in my_module
    @http.route('/report/download_document/<reportname>/<docids>', type='http', auth="user")
    @serialize_exception
    def download_document(self, **kw):
        *some code here*
        if <condition>:
           *some code here*
           return *something here*
        else:
           return super(ReportControllerProject, self).download_document(**kw)

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

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