简体   繁体   中英

How to override existing methods by custom module in odoo

Below method is existing in account.move.line

def _default_get_move_form_hook(self, cursor, user, data):
    data = super(account_move_line, self)._default_get_move_form_hook(cursor, user, data)
    if data.has_key('analytics_id'):
        del(data['analytics_id'])
    return data

I want to remove its functionality by override it in my custom module. Can anyone please tell me, how can I do it. I am trying below code:

@api.multi
def _default_get_move_form_hook(self, cr, uid, context=None):
    if self.pool['res.users'].has_group(cr, uid, 'base.group_erp_manager'):
        res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','LAL')], context=context)
        return res and res[0] or False
    elif self.pool['res.users'].has_group(cr, uid, 'purchase.group_purchase_user'):
        res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','SAS')], context=context)
        return res and res[0] or False

in you new module that inherit from the account.move.line dont use the new api then use the old one every method need 4 argument

def _default_get_move_form_hook(self, cr, uid, context=None):
    #put you custom code here

or just redifine the method with same name this should overrid it

If you want to remove its functionality by override it in your custom module.

You need to maintain the signature of method same as it is in original so

def _default_get_move_form_hook(self, cr, uid, data):

instead of

def _default_get_move_form_hook(self, cr, uid, context=None):

Later you need to make sure that your method should get called instead of original method, how ? follow the steps..

  • you need to make sure that you are not making a super call to original method. and also
  • search for the original method is being called from the module or part of odoo,
  • replace the method getting called from original class in original module by your class in your module

try this .. .

Thanks!

Update:

you need to make sure that the method you are going to override, is using the same Odoo version style,

All OpernERP v7 methods can't accepts odoo v8 style, so I have updated my answer by removing @api.multi

Direct conversion of OpenERP V7 method -> Odoo V8 with decorator doesn't always guarantee to work as normal, so best practice is the class and method structure you need to make sure to be same as original

To override it you can use:

@api.multi
def _default_get_move_form_hook(self):
    #Custom code

The expected returned value of this method is a dict, so make sure you return a dict.

_default_get returns _default_get_move_form_hook result:

def _default_get(...):   
    ...  
    data = self._default_get_move_form_hook(cr, uid, data)  
    return data

And default_get calls default_get :

def default_get(self, cr, uid, fields, context=None):
    data = self._default_get(cr, uid, fields, context=context)
    for f in data.keys():
        if f not in fields:
            del data[f]
    return data

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