来自Odoo文档的Odoo api.model,什么是`model.env`?

时间:2018-03-06 19:12:40

标签: odoo odoo-10

我正在尝试定义方法error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]

@api.model

问题是,official documentation表示你可以使用@api.model def test(param1, param2): print model.env['product.template'].search([("company_id", "=", param1), ("warehouse_id", "=", param2), ]) ,但当我尝试这样做时,我得到:

model.env

用于获取NameError: global name 'model' is not defined 的权限import是什么?

1 个答案:

答案 0 :(得分:2)

model的含义是您需要一个模型对象(空记录集)才能访问env。在新API中,self将成为记录集,因此您可以通过self.env访问环境:

@api.model
def test(self, param1, param2):
    print self.env['product.template'].search([("company_id", "=", param1), ("warehouse_id", "=", param2), ])

请注意,该方法应在model class上定义(在您的代码段中,test方法签名缺少self参数)。

在相关的说明中,您提供的链接不是来自官方文档并且已过时 - 它是在从旧API到OpenERP 7.0和Odoo 8.0之间的新API的初始迁移期间编写的,因此它是不是最好的来源。我建议您参考Odoo.com上提供的官方文档。

相关问题