Search code examples
pythonodooodoo-8

NotImplementedError: 'update' not supported on frozendict - Odoo v8


I have this code on my Odoo v8 module:

@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

This is a button action, but when I click it it throws me:

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

Has anybody encounter this kind of error implementing this?

I think it has to do with the order in which the context is called, but Im not really sure.


Solution

  • To update context try this.

    context = self.env.context.copy()
    context.update({'domain':[('something','=','something')]})
    

    Now use this as your context variable.

    UPDATE:

    The above solution is appropriate for the use case described in this question. However there are many circumstances in Odoo where context is taken from the environment and the above described answer does not really explain how to update the context in that fashion. So in those circumstances you will want to use the with_context() function as described by others on this posting.

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

    In this circumstance self is the object in question which could vary. You can find many examples of with_context() in the Odoo source code.