从另一个类调用函数-字段-Odoo v8 [英] Call function from another class - field - Odoo v8

查看:113
本文介绍了从另一个类调用函数-字段-Odoo v8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此感到挣扎,对此我不太清楚.

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"), 
    }

但是这不起作用,对此我感到非常困惑,如何从Odoov8中的另一个对象调用函数?

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

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

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

有什么想法吗?

提前谢谢!

推荐答案

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

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

在新的API中,引入了环境的概念.其主要 目的是提供围绕游标user_id的封装, 模型,上下文,记录集和缓存

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()仍然存在,也无需使用为了向后兼容

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是类似于字典的对象,用于存储Odoo模型的实例和其他信息,以便您可以从任何Odoo模型访问其他对象及其方法.

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.

这篇关于从另一个类调用函数-字段-Odoo v8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆