预期单例:以编程方式创建新发票时的 res.users()

发布于 2025-01-14 20:13:01 字数 351 浏览 3 评论 0原文

我正在尝试在控制器中创建新发票:

request.env['account.move'].sudo().create([
    {
        'move_type': 'out_invoice',
        'journal_id': 1,
        'invoice_date': '2020-01-10',
        'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
    }])

但我收到此错误

预期单例:res.users()

I'm trying to create a new invoice in controller:

request.env['account.move'].sudo().create([
    {
        'move_type': 'out_invoice',
        'journal_id': 1,
        'invoice_date': '2020-01-10',
        'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
    }])

But i got this error

Expected singleton: res.users()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2025-01-21 20:13:01

我在领导的帮助下找到了答案,如下:

这个问题是因为我们缺少 self.env.user,为此,我们必须添加 .with_user(SUPERUSER_ID ) 模型调用后。

例子:

from odoo import SUPERUSER_ID

request.env['account.move'].with_user(SUPERUSER_ID).sudo().create([
    {
        'move_type': 'out_invoice',
        'journal_id': 1,
        'invoice_date': '2020-01-10',
        'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
    }])

I've found the answer with the help of my leader, here it is:

This issue is because we missing self.env.user, to do this, we must add .with_user(SUPERUSER_ID) after model call.

Example:

from odoo import SUPERUSER_ID

request.env['account.move'].with_user(SUPERUSER_ID).sudo().create([
    {
        'move_type': 'out_invoice',
        'journal_id': 1,
        'invoice_date': '2020-01-10',
        'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
    }])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文