IndexError:元组索引超出范围 - Odoo v8到Odoo v10社区

时间:2017-03-22 03:24:49

标签: python openerp odoo-8 odoo-10

经过一些修改,并将read()更改为browse()后,我的方法如下所示:

@api.multi
def update_url(self): 
    data = self.env['wizard.seniat.url'].browse()[0] 
    url_obj = self._get_url() # cr ,uid, ids, context
    url_obj.write(
        {'name': data and data['url1'],
         'url_seniat': data and data['url2'],
         'url_seniat2': data and data['url3']})
    return {}

最初,在v8上它看起来像这样:

def update_url(self, cr, uid, ids, context=None):
    data = self.pool.get('wizard.seniat.url').read(cr, uid, ids)[0]
    url_obj = self._get_url(cr, uid, ids, context)
    url_obj.write(
        {'name': data['url1'],
         'url_seniat': data['url2'],
         'url_seniat2': data['url3']})
    return {}

这是从v8迁移到v10,社区

当我点击按钮时,它会抛出这个:

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 866, in call_button
action = self._call_kw(model, method, args, {})
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 672, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/wizard/wizard_url_seniat.py", line 74, in update_url
data = self.env['wizard.seniat.url'].browse()[0] 
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 5175, in __getitem__
return self._browse((self._ids[key],), self.env)
IndexError: tuple index out of range

我仍然无法弄清楚,有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我在 update_url()中发现了问题。您应该在浏览()中提及要浏览的ID,但在您的情况下,您将拥有自己的当前记录对象。

@api.multi
def update_url(self): 
    # No need to browse because you have already current record in self 
    # data = self.env['wizard.seniat.url'].browse()[0]  
    url_obj = self._get_url() # cr ,uid, ids, context
    url_obj.write(
        {'name': self.url1 or False,
         'url_seniat': self.url2 and False,
         'url_seniat2': self.url3 and False})
    return {}

这可能会对你有帮助。

答案 1 :(得分:1)

您必须使用ID列表才能使用browse,正如Jignesh Jarsaniya在答案中已经说明的那样。顺便说一下,这个模块真的很难理解,因此难以迁移。

我会像以下一样迁移此方法:

@api.multi
def update_url(self):
    for url in self:
        url_obj = url._get_url()
        url_obj.write({
            'name': url.url1,
            'url_seniat': url.url2,
            'url_seniat2': url.url3
        })
    return {}

但这可能是错的,因为我不知道_get_url是什么或返回值是什么样的。但这种方法似乎是多记录可用的,所以为什么不使它可以多记录使用。如果不需要或者不允许,请将其迁移为:

@api.multi
def update_url(self):
    self.ensure_one()
    url_obj = self._get_url()
    url_obj.write({
        'name': self.url1,
        'url_seniat': self.url2,
        'url_seniat2': self.url3
    })
    return {}