2017-02-27 65 views
1

我移植一些模塊從V7到V10ValueError異常:預計單:res.partner - Odoo 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來?

任何想法?

回答

1

預期的Singleton:

類方法需要單個調用對象(單可瀏覽記錄)調用該方法,並假設它會通過多個調用的對象(可瀏覽記錄集)調用然後方法不能以確定它應該處理哪個對象,因此它會產生一個錯誤期望的單身人士

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

@ api.one

這個裝飾上的記錄集的記錄自動循環給你。自我被重新定義爲當前記錄

注意: 注意:返回的值放在列表中。這並不總是被Web客戶端支持,例如,按鈕動作 方法。在這種情況下,您應該使用@ api.multi來修飾您的方法,並且可能會調用self.ensure_one() in 的方法定義。

@ api.multi

自將不迭代當前Recordset。這是默認行爲(多個可瀏覽的對象)。返回非premitive類型的數據(列表,字典,函數)方法必須裝飾@ api.multi

@ api.model

這個裝飾將舊的API調用轉換爲裝飾功能,新API簽名。它允許在 遷移代碼時有禮貌。自身不包含由此裝飾器裝飾的方法中的任何記錄/記錄集。

所以,簡單地調用此類

self.env [ '模型名稱']。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

+0

非常感謝你CH – NeoVe

1
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