NameError:全局名称' cr'未定义 - 从Odoov7迁移到Odoov10

时间:2017-02-24 19:57:03

标签: python openerp

我在OpenERP v7上有这个向导:

class UpdateInfoPartner(models.TransientModel):
_name = 'update.info.partner'

def update_info(self):

    context = dict(self._context or {})
    seniat_url_obj = self.pool.get('seniat.url')
    cr.execute('''SELECT id FROM res_partner WHERE vat ilike 'VE%';''')
    record = cr.fetchall()
    pids = [item[0] for item in record]
    seniat_url_obj.connect_seniat(pids, all_rif=True)
    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/update_info_partner.py", line 14, in update_info
cr.execute('''SELECT id FROM res_partner WHERE vat ilike 'VE%';''')
NameError: global name 'cr' is not defined

我知道这种方式调用queries是新API的问题,但我怎样才能在v10上替换它?它不完全像v8

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我用self.env.cr解决了这个问题:

class UpdateInfoPartner(models.TransientModel):
    _name = 'update.info.partner'

def update_info(self):

    context = dict(self._context or {})
    seniat_url_obj = self.pool.get('seniat.url')
    self.env.cr.execute('''SELECT id FROM res_partner WHERE vat ilike 'VE%';''')
    record = self.env.cr.fetchall()
    pids = [item[0] for item in record]
    seniat_url_obj.connect_seniat(pids, all_rif=True)
    return{}