Odoo 9,如何使用 self.pool.get 获取记录 ID?
Odoo 9, How can I get a record ID using self.pool.get?
我正在为 Odoo 9 开发一个模块,我有一个名为 "job_id" 的字段。这应该是一个唯一的自动递增整数字段,用于帮助识别我的记录。我有另一个名为 "account_id" 的字段,它是一个与 res.partner 相关的 Many2One。 job_id 仅对 account_id.
是唯一的
我的目标是计算 job_id 并存储 以备将来使用;当 account_id 更改或创建表单时。目前,我是 运行 创建方法中的函数。
在 Odoo shell 中,我发现如果我做了类似于下面的事情,它就起作用了,但是当我将它添加到我的模块时,它出错说模型没有 "env"属性。
def make_job_id(self, cr, uid, vals, context=None):
if context is None:
context = {}
job_ids = []
ids = self.env['custom.custom'].search([('account_id','=',vals['account_id'])]) # Search the custom.custom model for all records who's account_id matches this record's account_id and return object IDs
fields = ids.fields_get('job_id') # Get python field descriptor, always returns id + whatever specified fields
jobs = ids.read(fields) # Read the value of the supplied fields for each record
if len(jobs) > 0:
for i in jobs:
if i.job_id not in job_ids:
job_ids.append(i.job_id)
job_ids = max(job_ids) + 1
else:
job_ids = 1
vals['job_id'] = job_ids
return vals['job_id']
在发现 Odoo 显然对 env 何时可用很挑剔之后,我将函数修改为以下内容。
我正在做
ids = self.pool.get('custom.custom').search(cr, uid, [('account_id','=',vals['account_id'])], context=context)
而不是 ids = self.env bla bla bla
我的函数没有任何其他变化,只是添加了调试行,这些调试行只是将所有这些变量的值输出到日志中。
当我创建第一条记录时,我没有遇到任何问题,我的日志显示如下:
2017-01-24 18:49:23,211 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, the_ids: []
2017-01-24 18:49:23,212 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, job_ids: 1
2017-01-24 18:49:23,212 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, vals[job_id]: 0
2017-01-24 18:49:23,213 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, vals[job_id]: 1
然而,当为该帐户创建新记录时,我得到以下引用:
2017-01-24 18:50:05,412 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, the_ids: [1]
2017-01-24 18:50:05,414 7875 ERROR [redacted database] openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 680, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 316, 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 309, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 959, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 509, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 892, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 884, 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/custom/models/models.py", line 593, in create
vals['job_id'] = make_job_id(self, cr, uid, vals, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/addons/custom/models/models.py", line 569, in make_job_id
the_fields = the_ids.fields_get('job_id')
AttributeError: 'list' object has no attribute 'fields_get'
我看到的是 "self.pool.get" 不会 return id 像 "self.env" 的对象会,而只是 return是ID。
我可以对我的函数做些什么才能使用 self.pool.get 提供的 ID?
正如您提到的 "search" 方法 returns 只有 ID 而不是对象。尝试在 "search" 之后使用 "browse" 方法来获取实际对象,如下所示:
objs = self.pool.get('custom.custom').browse(cr, uid, ids, context=context)
我正在为 Odoo 9 开发一个模块,我有一个名为 "job_id" 的字段。这应该是一个唯一的自动递增整数字段,用于帮助识别我的记录。我有另一个名为 "account_id" 的字段,它是一个与 res.partner 相关的 Many2One。 job_id 仅对 account_id.
是唯一的我的目标是计算 job_id 并存储 以备将来使用;当 account_id 更改或创建表单时。目前,我是 运行 创建方法中的函数。
在 Odoo shell 中,我发现如果我做了类似于下面的事情,它就起作用了,但是当我将它添加到我的模块时,它出错说模型没有 "env"属性。
def make_job_id(self, cr, uid, vals, context=None):
if context is None:
context = {}
job_ids = []
ids = self.env['custom.custom'].search([('account_id','=',vals['account_id'])]) # Search the custom.custom model for all records who's account_id matches this record's account_id and return object IDs
fields = ids.fields_get('job_id') # Get python field descriptor, always returns id + whatever specified fields
jobs = ids.read(fields) # Read the value of the supplied fields for each record
if len(jobs) > 0:
for i in jobs:
if i.job_id not in job_ids:
job_ids.append(i.job_id)
job_ids = max(job_ids) + 1
else:
job_ids = 1
vals['job_id'] = job_ids
return vals['job_id']
在发现 Odoo 显然对 env 何时可用很挑剔之后,我将函数修改为以下内容。
我正在做
ids = self.pool.get('custom.custom').search(cr, uid, [('account_id','=',vals['account_id'])], context=context)而不是
ids = self.env bla bla bla
我的函数没有任何其他变化,只是添加了调试行,这些调试行只是将所有这些变量的值输出到日志中。
当我创建第一条记录时,我没有遇到任何问题,我的日志显示如下:
2017-01-24 18:49:23,211 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, the_ids: [] 2017-01-24 18:49:23,212 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, job_ids: 1 2017-01-24 18:49:23,212 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, vals[job_id]: 0 2017-01-24 18:49:23,213 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, vals[job_id]: 1
然而,当为该帐户创建新记录时,我得到以下引用:
2017-01-24 18:50:05,412 7875 DEBUG [redacted database] openerp.addons.custom.models.models: ####### make_job_id, the_ids: [1]
2017-01-24 18:50:05,414 7875 ERROR [redacted database] openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 680, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 316, 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 309, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 959, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 509, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 892, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 884, 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/custom/models/models.py", line 593, in create
vals['job_id'] = make_job_id(self, cr, uid, vals, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/addons/custom/models/models.py", line 569, in make_job_id
the_fields = the_ids.fields_get('job_id')
AttributeError: 'list' object has no attribute 'fields_get'
我看到的是 "self.pool.get" 不会 return id 像 "self.env" 的对象会,而只是 return是ID。
我可以对我的函数做些什么才能使用 self.pool.get 提供的 ID?
正如您提到的 "search" 方法 returns 只有 ID 而不是对象。尝试在 "search" 之后使用 "browse" 方法来获取实际对象,如下所示:
objs = self.pool.get('custom.custom').browse(cr, uid, ids, context=context)