【问题标题】:Call python object - odoo 9调用 python 对象 - odoo 9
【发布时间】:2016-11-25 11:16:27
【问题描述】:

在 python 控制台(jupyter)中,我使用以下形式的 python 库:

class SomeClass(object) 
    def __init__(self, arg1, arg2):
        ... 
    def fct1(self):
        ...
    return something

而且我创建一个对象没有问题:

x = SomeClass(arg1,arg2)

我想在 Odoo 中使用这些方法。 我尝试了以下方法:

class SomeClass(**models.Model**)
    def **connect**(self, arg1, arg2):
        ...
    def fct1(self):
        ...
    return something

将“object”替换为“model,Models”,使其成为 odoo 类 + 用方法名称重命名 init

但是

x = connect(arg1,arg2)

返回:

NameError:未定义全局名称“connect”

如何在 Odoo(新 API)中使用我的 python 库?

TIA

更新:

我也试过打电话

x= self.connect(arg1,arg2) 或 x=SomeClass.connect(arg1,arg2)

但是当我“打印 x”时它返回“无”。我认为没有创建实例

【问题讨论】:

  • 如果你希望名字是 connect 你应该使用类名作为 connect 并且__init__ 是一个构造函数
  • tnx 用于 rply。恐怕我不明白你的回答。我更新了我的问题,也许它更清楚了。

标签: python python-2.7 odoo odoo-9


【解决方案1】:

感谢zbik的回答:

myaddons 文件夹中的 myclass.py

class MyClass:
    def __init__(self, name):
        self.name = name
    def _test(self,a,b):
        return a+b

在其他 Odoo 类中:

from openerp.addons.myaddons.myclass import MyClass
...
x = MyClass('Hello')
y = x._test(2,3)
...
print x.name
> Hello
print y
> 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多