onorua onorua - 3 months ago 27
Python Question

self.env for manually defined model raise an error

I'm trying to create country guess application for Odoo 9, based on barcode value, for this I've created following models:

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


But it doesn't work, and installation fails with error stack trace:

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'


I believe I'm missing something, but can't understand what exactly, any help will be highly appreciated.

Answer

You cannot make a dependency from another model filed in @api.depends. Decorator expects that you supply a list of current model fields:

Each argument must be a string that consists in a dot-separated sequence of field names:

So odoo orm is unable to find res field in product.template.

Comments