簡體   English   中英

ValueError:預期的單例:res.partner-Odoo v10社區

[英]ValueError: Expected singleton: res.partner - Odoo v10 community

我正在將某些模塊從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會更容易嗎?

有任何想法嗎?

預期的Singleton:

需要類方法單個調用對象(單可瀏覽記錄)調用的方法,並假設它會被多個調用對象調用(可瀏覽記錄集),則方法是不能夠識別哪個對象它應處理,因此它會提高預期的Singleton錯誤。

新的API裝飾器用於定義方法調用模式,無論方法僅允許單個對象還是多個對象來調用此方法。

@ api.one

該裝飾器自動為您在RecordSet的Records上循環。 將自身重新定義為當前記錄

注意:注意:返回值放在列表中。 Web客戶端並不總是支持此功能,例如按鈕操作方法。 在這種情況下,您應該使用@ 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

參考鏈接

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM