手动定义模型的self.env引发错误

时间:2016-08-17 15:52:30

标签: python openerp odoo-9

我试图根据条形码值为Odoo 9创建国家猜测应用程序,为此我创建了以下模型:

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")

但它不起作用,安装失败,错误堆栈跟踪:

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'

我相信我错过了一些东西,但无法理解究竟是什么,任何帮助都会受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

您无法从@api.depends中提交的其他模型中获取依赖关系。 Decorator希望您提供当前模型字段的列表:

  

每个参数必须是一个由点分隔的字段名称序列组成的字符串:

因此,odoo orm无法在res中找到product.template字段。