我在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
这是一个按钮动作,但是当我点击它时,它会抛出:
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
有人在实现这个时遇到过这种错误吗?
我认为这与上下文调用的顺序有关,但我不太确定
要更新上下文,请尝试这样做。
context = self.env.context.copy()
context.update({'domain':[('something','=','something')]})
现在使用这个作为你的上下文变量。
更新:上面的解决方案适用于这个问题中描述的用例。然而,在Odoo中有很多情况下,上下文是从环境中获取的,上面描述的答案并没有真正解释如何以这种方式更新上下文。因此,在这种情况下,您将需要使用本文中其他人描述的with_context()函数。
context = self.env.context.copy()
context.update({'domain':[('something','=','something')]})
self.with_context(context).your_function()
在这种情况下,self是可以变化的对象。您可以在Odoo源代码中找到许多with_context()的示例。
当然您可以复制上下文并使用您喜欢的方式,但是,当您复制forezedict时,它将产生新的字典打破当前上下文,而我会建议您使用with_context
方法。
self.with_context(key=value,key=value)
这将更新当前环境上下文并自动向前推进。
我有同样的问题,我在这里找到了这个解决方案
那么,这就是答案
context = dict(self.env.context)
context.update({key: value})
self.env.context = context