gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:44 25 4
gpt4 key购买 nike

我正在尝试根据条形码值为 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'

我相信我遗漏了一些东西,但无法理解到底是什么,我们将不胜感激任何帮助。

最佳答案

您不能依赖于 @api.depends 中提交的另一个模型. Decorator 期望您提供当前模型字段的列表:

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

所以odoo orm无法在product.template中找到res字段。

关于python - 手动定义模型的 self.env 引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com