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

时间:2017-03-21 20:33:30

标签: python openerp odoo-8 odoo-10

我有这个方法:

class WizNroctrl(models.TransientModel):
    _name = 'wiz.nroctrl'
    _description = "Wizard that changes the invoice control number"

    name = fields.Char(string='Control Number', size=32, required=True) #32, non keyword error was after string
    sure = fields.Boolean(string='Are you sure?')

    @api.multi
    def set_noctrl(self): #, cr, uid, ids, context=None
        """ Change control number of the invoice
        """
        #if context is None:
            #context = {}
        data = self.env['wiz.nroctrl'].read()[0] # cr, uid, ids
        if not data['sure']:
            raise UserError(
                _("Error!"),
                _("Please confirm that you want to do this by checking the"
                " option"))
        inv_obj = self.env['account.invoice']
        n_ctrl = data['name']

        inv_obj.write(self._context.get('active_id'), {'nro_ctrl': n_ctrl}) #cr, uid, context=context
        return {}

我不知道这是否需要self._ensure_one()声明,但每次点击它都会引发我的注意:

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_nro_ctrl.py", line 44, in set_noctrl
data = self.env['wiz.nroctrl'].read()[0]
IndexError: list index out of range

这是我从v8到v10社区进行本地化的一个镜像。

原始代码如下所示:

class WizNroctrl(osv.osv_memory):
    _name = 'wiz.nroctrl'
    _description = "Wizard that changes the invoice control number"

    def set_noctrl(self, cr, uid, ids, context=None):
        """ Change control number of the invoice
        """
        if context is None:
            context = {}
        data = self.pool.get('wiz.nroctrl').read(cr, uid, ids)[0]
        if not data['sure']:
            raise osv.except_osv(
                _("Error!"),
                _("Please confirm that you want to do this by checking the"
                " option"))
        inv_obj = self.pool.get('account.invoice')
        n_ctrl = data['name']

        inv_obj.write(cr, uid, context.get('active_id'), {'nro_ctrl': n_ctrl},
                    context=context)
        return {}

    _columns = {
        'name': fields.char('Control Number', 32, required=True),
        'sure': fields.boolean('Are you sure?'),
    }
WizNroctrl()

我对此行的疑问大多是data = self.env['wiz.nroctrl'].read()[0] [0]参数是否适用于新API?

关于我提出的另一个问题,有人指出,使用browse()优于read()任何想法应该更好吗?

1 个答案:

答案 0 :(得分:2)

from odoo import _, api, fields, models
from odoo.exceptions import UserError


class WizNroctrl(models.TransientModel):
    _name = 'wiz.nroctrl'
    _description = 'Wizard that changes the invoice control number'

    name = fields.Char(string='Control Number', size=32, required=True)
    sure = fields.Boolean(string='Are you sure?')

    @api.multi
    def set_noctrl(self):
        '''Change control number of the invoice.'''
        self.ensure_one()
        if not self.sure:
            raise UserError(
                _('Please confirm that you want to do this by checking the'
                ' option'))
        inv_obj = self.env['account.invoice']
        inv_obj.browse(self.env.context.get('active_id')).write({
            'nro_ctrl': self.name,
        })
        return {}

一些注意事项:

  • 从原始方法添加self.ensure_one(),看起来您只在一条记录上运行。
  • 在这种情况下,您不需要read()browse(),因为self已经是向导记录。
  • 假设这将通过向导上的按钮调用,执行return {'type': 'ir.actions.act_window_close'}以自动关闭窗口可能更为正确。