Odoo Help

Welcome!

This community 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.

1

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

By
test
on 4/1/13, 6:34 AM 3,750 views

The Question has been closed

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

Hello,

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
2

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

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.

test
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
2

Mohammad Alhashash

--Mohammad Alhashash--
2075
| 5 5 8
Giza, Egypt
--Mohammad Alhashash--

Developer

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

patrick

--patrick--
1958
| 5 4 7
Baarn, Netherlands
--patrick--
patrick
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.

Example:

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 community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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