gpt4 book ai didi

python - 名称错误 : global name 'company_id' is not defined - Odoo v10 community

转载 作者:行者123 更新时间:2023-11-30 22:41:35 25 4
gpt4 key购买 nike

我正在将一些模块从 Odoo 社区版本的 v7 迁移到 v10。

现在,我修改了这个方法,它看起来像这样:

class ResPartner(models.Model):
_inherit = 'res.partner'

@api.multi
@api.depends('company_id')
def _get_country_code(self):
"""
Return the country code of the user company. If not exists, return XX.
"""
context = dict(self._context or {})
user_company = self.env['res.users'].browse(company_id)
return user_company.partner_id and user_company.partner_id.country_id \
and user_company.partner_id.country_id.code or 'XX'

但是每次我尝试转到 res.partner View 时,它都会抛出此错误:

Odoo Server Error

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 862, in call_kw
return self._call_kw(model, method, args, kwargs)
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/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 2995, in read
values[name] = field.convert_to_read(record[name], record, use_name_get)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 5171, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/fields.py", line 860, in __get__
self.determine_value(record)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/fields.py", line 969, in determine_value
self.compute_value(recs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/fields.py", line 924, in compute_value
self._compute_value(records)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/fields.py", line 918, in _compute_value
self.compute(records)
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/model/partner.py", line 72, in _get_uid_country
res = {}.fromkeys(self._get_country_code())
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/model/partner.py", line 51, in _get_country_code
user_company = self.env['res.users'].browse(company_id)
NameError: global name 'company_id' is not defined

我是 v10 API 新手

有什么想法吗?

最佳答案

它给你以下错误:

ValueError: Expected singleton: res.partner(1, 33, 8, 18, 22, 23)'

我认为,您已经在列表/ TreeView 中添加了字段。根据您当前的代码,您没有迭代记录集。

如果您想保留原始代码并进行改进self.company_id,那么我们应该从列表/ TreeView 中删除字段。除此之外,您可以继续我的回答。它会工作得很好。

尝试使用以下代码:

@api.depends('company_id')
def _get_country_code(self):
"""
Return the country code of the user company. If not exists, return XX.
"""
context = dict(self._context or {})
for partner in self:
user_company = self.env['res.company'].browse(self.company_id)
#NOTE: replace code name with your real field name where you want to see value
partner.code = user_company.partner_id and user_company.partner_id.country_id \
and user_company.partner_id.country_id.code or 'XX'

关于python - 名称错误 : global name 'company_id' is not defined - Odoo v10 community,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com