Odoo 9,如何使用self.pool.get获取记录ID?

时间:2017-01-24 21:24:22

标签: python-2.7 record self odoo-9

我正在为Odoo 9开发一个模块,我有一个名为" job_id"的字段。这应该是一个唯一的自动递增整数字段,用于帮助识别我的记录。我有另一个名为" account_id"的字段,它是与res.partner有关系的Many2One。 job_id仅对account_id是唯一的。

我的目标是将job_id计算为并存储以用于将来的增量;在account_id更改或创建表单时。目前,我在create方法中运行我的函数。

在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.env bla bla bla
我在做
ids = self.pool.get('custom.custom').search(cr, uid, [('account_id','=',vals['account_id'])], context=context)

除了添加简单地将所有这些变量的值输出到日志的调试行之外,我的函数没有其他任何改变。

当我创建我的第一条记录时,我没有遇到任何问题,我的日志显示了这一点:

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" 返回ID为" self.env"确实如此,而只是返回id。

我可以对我的函数做些什么来使用self.pool.get提供的id?

1 个答案:

答案 0 :(得分:0)

正如您所提到的,“搜索”方法仅返回ID而不返回对象。尝试在“搜索”之后使用“浏览”方法来获取实际对象,如下所示: objs = self.pool.get('custom.custom').browse(cr, uid, ids, context=context)