Call function from another class - field - Odoo v8

ぃ、小莉子 提交于 2019-12-23 05:49:06

问题


I'm struggling with this and I'm not so clear about it.

Let's say I have a function in a class:

class my_class(osv.Model):
    _name = 'my_class'
    _description = 'my description'

    def func (self, cr, uid, ids, name, arg, context=None):
            res = dict((id, 0) for id in ids)
    sur_res_obj = self.pool.get('contratos.user_input')
    for id in ids:
        res[id] = sur_res_obj.search(cr, uid,  # SUPERUSER_ID,
            [('contratos_id', '=', id), ('state', '=', 'done')],
            context=context, count=True)
    return res
    columns = {
        'function': fields.function(_get_func,
        string="Number of completed Contratos", type="integer"),
my_class()

Now I want to call this very same function from another class-object:

class another_class(osv.Model):
    _inherit = 'my_class'
    _name = 'my_class'

    columns = {
        'another_field' : fields.related('function', 'state', type='char', string = "Related field which calls a function from another object"), 
    }

But this isn't working, I'm very confused about this, how can I call a function from another object in Odoov8?

I've heard about self.pool.get but I'm not really sure on how to invoke it.

Any ideas?

Thanks in advance!


回答1:


Since you're using odoo8 you should use the new API. from the docs

In the new API the notion of Environment is introduced. Its main objective is to provide an encapsulation around cursor, user_id, model, and context, Recordset and caches

def my_func(self):
    other_object = self.env['another_class']
    other_object.create(vals) # just using create as an example

That means you don't need to explicity pass cr, uid, ids, vals and context in your methods anymore and you don't use self.pool.get() even though it's still there for backward compatibility

env is dictionary-like object that is used to store instances of the Odoo models and other information so you can access other objects and their methods from any Odoo model.




回答2:


def example(self, cr, uid, ids, context=None):
    otherClass = self.pool.get('my_class')
    ...
    otherClass.func(cr, uid, otherClassIds, name, arg, context)

More information.



来源:https://stackoverflow.com/questions/37242600/call-function-from-another-class-field-odoo-v8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!