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

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

我有这个方法:

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() 更好?有什么想法吗?

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'} 以自动关闭 window 可能更正确。