ValueError:预期的单例:res.partner - Odoo v10社区

时间:2017-02-27 19:17:14

标签: python openerp

我将某些模块从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会更容易吗?

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

预期的单身人士:

类方法需要单个调用对象(Single Browsable Record)来调用该方法并假设它将通过多个调用对象(可浏览记录集)调用然后方法不能确定它应该处理哪个对象,因此会引发错误预期单身

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

<强> @ api.one

这个装饰器会自动在RecordSet记录上循环。 Self被重新定义为当前记录

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

<强> @ api.multi

Self将是没有迭代的当前RecordSet。这是默认行为(多个可浏览对象)。返回非premitive类型数据(列表,字典,函数)的方法必须用 @ 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

答案 1 :(得分:2)

Please try this

    @api.multi
    def _get_country_code(self):
       code =False
       for partner in self:

          user_company = self.user_id
   #it will return the current login-ed user id 

          code = user_company.partner_id and user_company.partner_id.country_id and user_company.partner_id.country_id.code or 'XX'
       return code