gpt4 book ai didi

python - ValueError : Expected singleton: res. 伙伴 - Odoo v10 社区

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:00 27 4
gpt4 key购买 nike

我正在将一些模块从 v7 迁移到 v10

现在我有这些继承 res.partner

的方法
@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.users'].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'

@api.multi
def default_get(self, field_list):
""" Load the country code of the user company to form to be created.
"""
# NOTE: use field_list argument instead of fields for fix the pylint
# error W0621 Redefining name 'fields' from outer scope
context = {}
res = super(ResPartner, self).default_get(field_list)
res.update({'uid_country': self._get_country_code })
return res

@api.multi
def _get_uid_country(self):
""" Return a dictionary of key ids as invoices, and value the country code
of the user company.
"""
context = dict(self._context or {})
res = {}.fromkeys(self._get_country_code())
return res

每次尝试创建或读取合作伙伴时都会出现此错误:

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 73, 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(self.company_id)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/fields.py", line 854, in __get__
record.ensure_one()
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 4783, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: res.partner(1, 33, 8, 18, 22, 23)

我也在为此苦苦挣扎,您认为从 v8 而不是 v7 迁移到 v10 会更容易吗?

有什么想法吗?

最佳答案

预期的单例:

类方法需要单个调用对象(单个可浏览记录)来调用该方法,假设它将被多个调用对象(可浏览记录集)调用,则方法无法确定它应该处理哪个对象,因此它会引发错误 Expected Singleton

新的 API 装饰器用于定义方法调用模式,方法是否只允许单个对象或多个对象调用此方法。

@api.one

此装饰器会自动为您在 RecordSet 的 Records 上循环。 self 被重新定义为当前记录

注意:注意:返回值放在列表中。 Web 客户端并不总是支持这一点,例如按钮 Action 方法。在这种情况下,您应该使用 @api.multi 来装饰您的方法,并可能调用 self.ensure_one()方法定义。

@api.multi

Self 将是没有迭代的当前 RecordSet。这是默认行为(多个可浏览对象)。返回非前提类型数据(列表、字典、函数)的方法必须用 @api.multi

修饰

@api.model

这个装饰器会将旧的 API 调用转换为装饰函数到新的 API 签名。它允许礼貌时迁移代码。 Self 不包含此装饰器装饰的方法中的任何记录/记录集。

所以简单地这样调用

self.env['model_name'].method_name(参数)

尝试使用以下代码:

@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'

default_get() 方法 api 从 @api.multi 更改为 @api.model

Reference link

关于python - ValueError : Expected singleton: res. 伙伴 - Odoo v10 社区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42493993/

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