NotImplementedError:frozendict不支持'update' - Odoo v8

Neo*_*oVe 2 python openerp odoo-8

我的Odoo v8模块上有这个代码:

@api.multi
def button_generate_wh_doc(self):
    context = self._context
    partner = self.env['res.partner']
    res = {}
    for inv in self:
        view_id = self.env['ir.ui.view'].search([
            ('name', '=', 'account.invoice.wh.iva.customer')])
        context.update({
            'invoice_id': inv.id,
            'type': inv.type,
            'default_partner_id': partner._find_accounting_partner(
                inv.partner_id).id,
            'default_name': inv.name or inv.number,
            'view_id': view_id,
        })
        res = {
            'name': _('Withholding vat customer'),
            'type': 'ir.actions.act_window',
            'res_model': 'account.wh.iva',
            'view_type': 'form',
            'view_id': False,
            'view_mode': 'form',
            'nodestroy': True,
            'target': 'current',
            'domain': "[('type', '=', '" + inv.type + "')]",
            'context': context
        }
    return res
Run Code Online (Sandbox Code Playgroud)

这是一个按钮动作,但当我点击它时它会抛出我:

File "/home/user/odoov8/odoo-venezuela/l10n_ve_withholding_iva/model/invoice.py", line 427, in button_generate_wh_doc
'view_id': view_id,
File "/home/user/odoov8/odoo-8.0-20161017/openerp/tools/misc.py", line 1280, in update
raise NotImplementedError("'update' not supported on frozendict")
NotImplementedError: 'update' not supported on frozendict
Run Code Online (Sandbox Code Playgroud)

有没有人遇到这种错误实现这个?

我认为它与调用上下文的顺序有关,但我不确定.

Phi*_*ack 7

要更新上下文,请尝试以

context = self.env.context.copy()
context.update({'domain':[('something','=','something')]})
Run Code Online (Sandbox Code Playgroud)

现在使用它作为您的上下文变量.

更新:

上述解决方案适用于此问题中描述的用例.然而,在Odoo中有许多情况,其中从环境中获取上下文,并且上述答案并未真正解释如何以这种方式更新上下文.因此,在这种情况下,您将需要使用其他人在此帖子中描述的with_context()函数.

context = self.env.context.copy()
context.update({'domain':[('something','=','something')]})
self.with_context(context).your_function()
Run Code Online (Sandbox Code Playgroud)

在这种情况下,自我是有问题的对象,可能会有所不同.您可以在Odoo源代码中找到许多with_context()的示例.

  • 您的答案在技术上是正确的,但您应该使用with_context()作为第一个选项,如@ shodowsjedi的答案. (2认同)

ifi*_*hat 6

确保你可以复制上下文并使用你喜欢的方式但是,当你复制forezedict它会产生新的dict打破当前的上下文,而我会建议你使用with_context方法.

self.with_context(key=value,key=value)
Run Code Online (Sandbox Code Playgroud)

这将更新当前环境上下文并自动推进.