简体   繁体   中英

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

My core 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:

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.

I want this function to be used only in the inherit class of this module, not everywhere, so what am I gonna do?

The other modules will use the function of the module they are dependant on. So in the manifest of the module where you want to use the overridden function make sure to depend from my_module.

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

To resolve this situation, I just use condition and call super() function to execute this overriden funtion

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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