簡體   English   中英

IndexError:列表索引超出范圍-從Odoo v8到Odoo v10社區遷移

[英]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]新API的[0]參數正確嗎?

有人指出,在我提出的另一個問題上,最好是使用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'}以自動關閉窗口可能更正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM