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.

0

How do I call a model's method from another model?

By
Op
on 6/16/14, 8:50 PM 824 views

I would like to call a method of a model from anther model:

class mode1(osv.orm):

[...]

def method1(self, cr, uid, ids):

return 'Hello'

 

class model2(osv.orm):

_columns = {

  model1_relation = fields.many2one('model1')

[...]

def method2(self, cr, uid, ids):

  model2_item = self.browse(cr, uid, ids)

model2_item.model1_relation.method1()

 

3

Nehal

--Nehal--
2090
| 5 2 8
Ahmadabad, India
--Nehal--
Nehal
On 6/16/14, 11:50 PM

Hi,

Try the following : 
def method2(self, cr, uid, ids):
    model2_item = self.browse(cr, uid, ids)
  
  self.pool.get('model1_relation').method1(cr, uid, model2_item.id)

0
klacus
On 6/17/14, 12:06 AM

Hi.

The good example in sale.py in

def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,...

..

 

product_obj = self.pool.get('product.product')

..

max(0,product_obj.qty_available), product_obj.uom_id.name)

if you check the product/product.py you can find the definition.

    _columns = {
        'qty_available': fields.function(_product_qty_available, type='float', string='Quantity On Hand'),

looking more in product.py

_product_qty_available = _get_product_available_func(('done',), ('in', 'out')   

def _get_product_available_func(states, what):
        def _product_available(self, cr, uid, ids, name, arg, context=None):
            return {}.fromkeys(ids, 0.0)
        return _product_available

    )

 

A really nice object call, :-)

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/16/14, 8:50 PM
Seen: 824 times
Last updated: 3/16/15, 8:10 AM