0
votes

I am developing a module for Odoo 9, and I have a field called "job_id". This is supposed to be a unique auto-incrementing integer field that is used to help identify my records. I have another field called "account_id", which is a Many2One with a relation to res.partner. The job_id is unique only to account_id.

My goal is for the job_id to be calculated and stored for future increments; either when the account_id changes or when the form is created. At the moment, I'm running my function inside the create method.


Inside the Odoo shell, I found that if I did something similar to below, it worked but when I added it to my module, it errored saying that the model does not have an "env" attribute.

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']

After finding that apparently Odoo is picky about when exactly env is available, I modified my function to below.

Instead of

ids = self.env bla bla bla
I am doing
ids = self.pool.get('custom.custom').search(cr, uid, [('account_id','=',vals['account_id'])], context=context)

Nothing else has changed with my function except for adding debugging lines that simply output the values of all these variables to the log.


When I create my first record, I get no problems and my log shows this:

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

However when creating a new record for that account, I get the following trackback:

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'

What I am seeing is that "self.pool.get" does not return the object with id like "self.env" does, but instead simply returns the id.

What can I do to my function to be able to use the id provided by self.pool.get?

1

1 Answers

0
votes

As you mentioned "search" method returns only the ids and not the objects. Try to use "browse" method after "search" to get the actual objects, like this: objs = self.pool.get('custom.custom').browse(cr, uid, ids, context=context)