Thursday, 7 July 2016

Error when override orm write method in Odoo 8 or 9

If you try override write method with following new API:

         @api.model
         def write(self, vals):
            ....
            return super(MyClass, self).write(vals)

You will see the following error:

        Traceback (most recent call last):
          File "/opt/odoo/odoo-server/openerp/http.py", line 530, in _handle_exception
            return super(JsonRequest, self)._handle_exception(exception)
          File "/opt/odoo/odoo-server/openerp/http.py", line 567, in dispatch
            result = self._call_function(**self.params)
          File "/opt/odoo/odoo-server/openerp/http.py", line 303, in _call_function
            return checked_call(self.db, *args, **kwargs)
          File "/opt/odoo/odoo-server/openerp/service/model.py", line 113, in wrapper
            return f(dbname, *args, **kwargs)
          File "/opt/odoo/odoo-server/openerp/http.py", line 300, in checked_call
            return self.endpoint(*a, **kw)
          File "/opt/odoo/odoo-server/openerp/http.py", line 796, in __call__
            return self.method(*args, **kw)
          File "/opt/odoo/odoo-server/openerp/http.py", line 396, in response_wrap
            response = f(*args, **kw)
          File "/opt/odoo/odoo-server/addons/web/controllers/main.py", line 939, in call_button
            action = self._call_kw(model, method, args, {})
          File "/opt/odoo/odoo-server/addons/web/controllers/main.py", line 927, in _call_kw
            return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
          File "/opt/odoo/odoo-server/openerp/api.py", line 241, in wrapper
            return old_api(self, *args, **kwargs)
          File "/opt/odoo/odoo-server/openerp/api.py", line 239, in wrapper
            return new_api(self, *args, **kwargs)
        TypeError: write() got an unexpected keyword argument 'context'

Question: How to resolved mentioned error ?

Solution:

The method write should be decorated with decorator @api.multi. The mapping new API → old API defined by @api.model is inadequate (argument ids is missing).

Try with following code:

         @api.multi
         def write(self, vals):
            ....
            return super(MyClass, self).write(vals)

For more details Odoo documentation 

1 comment:

ImportError: cannot import name 'utils' from 'PyPDF2'

Odoo 15: Traceback (most recent call last):   File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner     self...