是否可以在不登录 Odoo 的情况下在控制器中使用 ORM 方法?

Is it possible to use ORM methods in a controller without logging in Odoo?

我正在向内部带有 link 的用户发送电子邮件(发送至控制器)。 link 是下一个:http://localhost/my-controller?my_variable=hello。为了接收变量 hello,我创建了下一个控制器:

class mail_controller(http.Controller):
    @http.route('/my-controller', type='http')
    def index(self, **args):
        my_variable = args.get('my_variable', False)
        if not my_variable:
            return 'Invalid URL'
        return '<h1>This is the received variable:</h1>' + str(my_variable)

一切都很完美。但是现在,我想使用一些 ORM 方法。为此,我将不得不做这样的事情:

connection = openerplib.get_connection(hostname='localhost',
                                       database='my_database',
                                       login='my_user',
                                       password='my_pwd', port=8069)

我想知道是否有任何方法可以避免这种使用 ORM 方法(或 Odoo 模型中声明的其他方法)的连接。首先,因为使用纯文本形式的用户和密码不太安全,其次,因为连接的参数很容易更改(例如,如果我将模块安装在其他数据库中,或者如果用户修改了他们的密码)。

有人能帮帮我吗?还有其他更好的控制器吗?

只需使用 http.request.env,这与您可能从模型方法中了解到的 self.env 相同。

您可以执行 your_objects = http.request.env['your.model'].search([]) 之类的操作以及您通常使用 Odoo ORM 执行的所有其他操作。只需将 self.env 替换为 http.request.env 即可。

您可以阅读更多关于 http.request in the documentation