初始化函数odoo 8中的环境模型

Environment model in init function odoo 8

我有个小疑问。我是 odoo 8 的新手。所以在我的模型中,我使用 self.env['#model'] 来访问特定模型。现在我的模型中有大约 10 个不同的函数,在每个模型中我都使用模型环境引用来引用其他两个模型。下面是代码:

def test(self):
     location = self.env['stock.location']
     # i get values from database models

def test1(self):
     location = self.env['stock.location']
     # i get values from database models

现在我需要两个不同功能的相同环境。 有没有像__init__函数这样的方法,它会初始化模型对象,我们可以在所有函数中使用它。

谢谢,

当然你可以将它设置为 class 的 属性 和 self 像这样

def test(self):
     self.location_obj = self.env['stock.location']
     # i get values from database models

def test1(self):
     # You can use self.location in this method and other methods
     self.location_obj.search([('id', '=', 1)])

从现在开始,您可以在 class 中的任何方法中访问 self.location(静态方法除外。但您不需要那个)