从另一个 class 调用函数 - 字段 - Odoo v8

Call function from another class - field - Odoo v8

我正在为此苦苦挣扎,对此我不是很清楚。

假设我在 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()

现在我想从另一个 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"), 
    }

但这行不通,我对此很困惑,如何从 Odoov8 中的另一个对象调用函数?

我听说过 self.pool.get 但我不太确定如何调用它。

有什么想法吗?

提前致谢!

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

More information.

由于您使用的是 odoo8,因此您应该使用新的 API。来自文档

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

这意味着您不再需要在您的方法中显式传递 cruididsvalscontext,并且您不要使用 self.pool.get(),即使它仍然存在是为了向后兼容

env 是类似字典的对象,用于存储 Odoo 模型的实例和其他信息,因此您可以从任何 Odoo 模型访问其他对象及其方法。