2017-03-21 3 views
1

에 Odoo의 V8 나는이 방법이 :이 self._ensure_one() 선언을 요구하는 경우IndexError : - 범위를 벗어난리스트 인덱스 Odoo V10 커뮤니티 마이그레이션

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 {} 

는 나도 몰라,하지만 나는 그것을 때마다 클릭 그것은 나를 던졌습니다 :

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() 이상 사용하는 것이 좋습니다.

답변

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() 원래 방법에서한다.
  • self은 이미 마법사 레코드이므로 read() 또는 browse()이 필요하지 않습니다.
  • 마법사에서 단추를 통해 호출한다고 가정하면 자동으로 창을 닫으려면 return {'type': 'ir.actions.act_window_close'}을 수행하는 것이 더 정확할 수 있습니다.
+0

멋진 소식을 전 해주셔서 감사합니다. 환호 – NeoVe

+1

@Naglis Jonaitis : 반환 '없음'도 팝업을 닫습니다. – CZoellner

관련 문제