Odoo Help


Can I able to inherit only one method? [Closed]

on 4/1/13, 6:34 AM 4,611 views

The Question has been closed

Sudhir Arya (SA)
on 04/18/2013 05:18:49


As we inherit particular field from target model, like this, _inherits = {'resource.resource':"resource_id"}

can I able to inherits a method only from targeted model?

_inherits = {'target.model':"_get_amount_calc"}

Does it possible ?

Note that you do not "inherit particular field", the field you specified, resource_id, should be the relational field in your model that links to the target model. All target model fields will be accessible from the new model.

Mohammad Alhashash
on 4/1/13, 10:36 AM
Sudhir Arya (SA)
On 4/1/13, 6:53 AM

It's called method overriding. You can override any method of inherited class.

For example I want to override def _amount_all() of sale.order in my module:

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
    #Todo code
    return res

If I want to call super(actual method in sale.order) I can call it by super keyword.

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
    #Todo code
    super(sale_order, self)._amount_all(cr, uid, ids, field_name, arg, context=None)
    return res

You can refer Method Overriding.

My Question is, Can we able to inherit method via _inherits = {'target.model':"_get_amount_calc_method"}

only one method from targeted model, as we inherits only one field.

on 4/1/13, 7:53 AM

No. you can not. There is no term like inherit method. But what you are saying is called method overriding which is possible.

Sudhir Arya (SA)
on 4/1/13, 8:35 AM
Mohammad Alhashash
On 4/1/13, 7:58 AM

No. Inheritance using _inherits only adds the fields to the new model. Actually you cannot "inherit particular field", all target model fields will be accessible from the new model.

The example field you specified, resource_id, must be the relational field in your model that links (many2one) to the target model record. You have to explicitly declare it in _columns.

If you want to access a method from the inherited model, you need to call it through the relation field.

For example, if you need to call the method target.model._get_amount_calc() in your method source.model.action1(), you can do something like this:

class source_model(osv.Model):
    _inherits = {'target.model':'resource_id'}
    def action1(self, cr, uid, ids, context=None):
        context = context or {}
        for line in self.browse(cr, uid, ids, context=context):
            amount = line.resource_id._get_amount_calc()
On 4/1/13, 6:56 AM

It is possible to inherit just a method from a specific model.

You start with the regular inherit command (_inherit = 'model.model')

Than you copy the method definition you want to inherit.


class res_partner(osv.osv):

_inherit = 'res.partner'

def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):

  Do your thing

  res = super(res.partner, self).name_search(cr, uid, name, args = args, operator = 'ilike', context = context)

  do more things

  return res

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 4/1/13, 6:34 AM
Seen: 4611 times
Last updated: 3/16/15, 8:10 AM