Search code examples
pythonodooodoo-8odoo-10

IndexError: list index out of range - Odoo v8 to Odoo v10 community


I have this class which inherits res.partner with name_search method:

class ResPartner(models.Model):
    _inherit = 'res.partner'

    @api.multi
    def name_search(self, name='', args=None, operator='ilike', limit=80): #cr, uid, context=None,  
    """
    Gets el id of the partner with the vat or the name and return the name
    """
    #if context is None:
        #context = {}
        self.ensure_one()
        args = args or []
        ids = []
        if len(name) >= 2:
            ids = self.search([('vat', operator, name)] + args, limit=limit) #cr, uid, [('vat', operator, name)] + args, limit=limit, context=context
        if not ids:
            ids = self.search([('name', operator, name)] + args, limit=limit) #cr, uid, [('name', operator, name)] + args, limit=limit, context=context
        return self.name_get() #cr, uid, ids, context=context

But every time I go to create an invoice account.invoice when trying to select a partner it throws me this:

Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 862, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 854, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 681, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 668, in call_kw_multi
ids, args = args[0], args[1:]
IndexError: list index out of range

I have some other methods on that class, but the offending one is the one above.

Originally this method looked like this (On version 8):

def name_search(
        self, cr, uid, name='', args=None, operator='ilike', context=None,
        limit=80):
    """
    Gets el id of the partner with the vat or the name and return the name
    """
    if context is None:
        context = {}
    args = args or []
    ids = []
    if len(name) >= 2:
        ids = self.search(cr, uid, [('vat', operator, name)] + args,
                          limit=limit, context=context)
    if not ids:
        ids = self.search(cr, uid, [('name', operator, name)] + args,
                          limit=limit, context=context)
    return self.name_get(cr, uid, ids, context=context)

Any ideas?


Solution

  • search in odoo return a recordSet try this:

        return ids and ids.name_get() or []
    

    EDITS example of how i do it :

        def _name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100, name_get_uid=None):
                """
                    override the _name_search to enable searching for the identity
                    by Arabic name and French name
                """
                args = []
                args += [
                    '|', '|', '|',
                    ('arabic_last_name', operator, name),
                    ('arabic_first_name', operator, name),
                    ('french_last_name', operator, name),
                    ('french_first_name', operator, name)
                ]
                access_rights_uid = name_get_uid or user
                ids = self._search(cr, user, args, limit=limit, context=context, access_rights_uid=access_rights_uid)
                res = self.name_get(cr, access_rights_uid, ids, context)
                return res