IndexError:列表索引超出范围 - Odoo v8到Odoo v10社区

时间:2017-05-02 18:10:26

标签: python openerp odoo-8 odoo-10

我有这个类使用res.partner方法继承name_search

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

但每次我在尝试选择合作伙伴时创建发票account.invoice时,都会抛出这个:

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

我在该课上有其他一些方法,但是有一种方法就是上面的方法。

最初这个方法看起来像这样(在版本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)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在odoo中搜索返回记录集试试这个:

    return ids and ids.name_get() or []

编辑我如何做的例子:

    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