2016-08-17 86 views
2

我試圖爲Odoo 9創建全國猜測應用,基於條碼值的誤差,爲了這個,我已經創建了以下型號:self.env手動定義的模型提高

class InstantProductTemplate(models.Model): 
    _name = 'product.template' 
    _inherit = 'product.template' 

    country_of_origin = fields.Many2one('res.country', 
             string='Country of origin', 
             compute="_guess_country", 
             store=True) 

    @api.one 
    @api.depends('barcode', 'res.country.ean_range') 
    # @api.onchange('barcode') 
    def _guess_country(self): 
     _logger.info("entered guess country!!") 
     ean_len = 13 
     if isinstance(self.barcode, (str, unicode)): 
      barcode_len = len(self.barcode) 
     else: 
      return 

     if barcode_len == ean_len: 
      barcode = self.barcode 
     elif barcode_len < ean_len: 
      barcode = '0' * (ean_len - barcode_len) + self.barcode 
     else: 
      # could not guess country of origin 
      return 

     prefix = int(barcode[:3]) 

     for ean in self.env['res.country.ean_range'].search([]): 
      if ean.range_start <= prefix <= ean.range_end: 
       self.country_of_origin = ean.country 
       break 


class InstantCountryRanges(models.Model): 
    _name = 'res.country' 
    _inherit = 'res.country' 

    ean_range = fields.One2many('res.country.ean_range', 'range_id') 


class InstantCountry(models.Model): 
    _name = 'res.country.ean_range' 

    name = fields.Char() 
    range_id = fields.Integer() 
    range_start = fields.Integer("Range start") 
    range_end = fields.Integer("Range end") 

但它不「T工作,並安裝失敗,錯誤堆棧跟蹤:

Traceback (most recent call last): 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 647, in _handle_exception 
    return super(JsonRequest, self)._handle_exception(exception) 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 684, in dispatch 
    result = self._call_function(**self.params) 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 320, in _call_function 
    return checked_call(self.db, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 118, in wrapper 
    return f(dbname, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 313, in checked_call 
    result = self.endpoint(*a, **kw) 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 963, in __call__ 
    return self.method(*args, **kw) 
    File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 513, in response_wrap 
    response = f(*args, **kw) 
    File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 901, in call_button 
    action = self._call_kw(model, method, args, {}) 
    File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 889, in _call_kw 
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 250, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/addons/base/module/module.py", line 459, in button_immediate_install 
    return self._button_immediate_function(cr, uid, ids, self.button_install, context=context) 
    File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 250, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/addons/base/module/module.py", line 533, in _button_immediate_function 
    registry = openerp.modules.registry.RegistryManager.new(cr.dbname, update_module=True) 
    File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 386, in new 
    openerp.modules.load_modules(registry._db, force_demo, status, update_module) 
    File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 338, in load_modules 
    loaded_modules, update_module) 
    File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 237, in load_marked_modules 
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks) 
    File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 136, in load_module_graph 
    registry.setup_models(cr, partial=True) 
    File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 203, in setup_models 
    model._setup_complete(cr, SUPERUSER_ID) 
    File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 250, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 354, in old_api 
    result = method(recs, *args, **kwargs) 
    File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 3081, in _setup_complete 
    field.setup_triggers(self.env) 
    File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 643, in setup_triggers 
    self._add_trigger(env, path_str) 
    File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 627, in _add_trigger 
    field = model._fields[name] 
KeyError: 'res' 

我相信我失去了一些東西,但不明白究竟是什麼,任何幫助將不勝感激。

+4

由於@ api.depends以字段名稱作爲參數,所以您不能寫'@ api.depends('barcode','res.country.ean_range')',而您給它''res.country.ean_range''這不是任何領域的名字 –

+0

謝謝你,不幸的是你評論的問題,而不是回答它,所以我不能正式「接受」你的解決方案。但它確實有幫助。 – onorua

回答

2

您不能依靠@api.depends中的其他模型提交依存關係。裝飾期待您提供當前模型字段列表:

每個參數必須是由字段名稱中的一個點分隔的序列串:

所以odoo ORM無法在product.template找到res字段。