← Back to team overview

openerp-expert-framework team mailing list archive

API change in trunk: m2o fields are now read() as (id, name) for osv_memory objects (bug 709567)

 

Hello everyone,

Following-up to bug 709567 [1] discussed a few weeks ago, the
osv_memory API has just been changed in trunk to make it more
consistent with regular osv objects.


*What is this about?*

Basically the osv_memory.read() method used to return bare integers for
many2one fields, and now it complies with the osv API by returning
pairs in the form (ID, Name).

The main benefit of this is to avoid an additional name_get() RPC call
from the client whenever such a m2o value needs to be displayed.
Another, less obvious reason to have it, is that the name_get call to
resolve the name of the target object can be performed with no access
right restriction when it is done on-the-fly during read. This is
useful for situations where the user does not have access to read the
destination table, but should be allowed to see the name of the record.
(e.g. displaying a res.partner with its related accounts, without being
authorized to actually read() account.account)
The rule is that it is allowed to see the name of records linked via
m2o relationships to any object you can see, and this is the reason
why it works.


*How does it affect you?*

You will probably need to adapt your osv_memory wizards to port them to
6.1, when the times comes. And as it is quite easy to make this change
in a backwards-compatible manner, why not do it now?

You need to check all the osv_memory wizards that call self.read() for
one reason or another. One place to start is to grep for "self.read("
in your addons/*/wizard directories.

To fix it, you have 2 obvious choices:
1. If you can directly use self.browse() instead of self.read(), please
do it, as it is more compatible and cleaner. It is also the recommended
way to access object data within an OpenERP module. read() is primarily
meant for RPC basic usage.
2. If you can't cleanly switch to self.browse(), e.g. because you need
to return some data as a read()-like dict, then you need to check the
result of the read() for m2o fields before using/returning them, when
that matters.
In the following dummy example, the last line adds compatibility with
the corrected API:
  for partner in self.read(cr, uid, 23, ['company_id'], context)
  company_id = my_partner['my_company']
  company_id = company_id[0] if isinstance(company_id, tuple) \
                             else company_id


*References*

The server revision that introduces this change is [2] and the
addons have been fully adapted to this new API at revision [3].

The rationale for doing this change in trunk only and not in 6.0 is
explained in comment #16 of [1], which also mentions a 6.0 server
branch that can be used to test or partially enable this in 6.0 [4].


[1] https://bugs.launchpad.net/openobject-server/+bug/709567
[2] http://bazaar.launchpad.net/~openerp/openobject-server/trunk/revision/3369 revid: odo@xxxxxxxxxxx-20110315132628-uhhpf95b9iyin482 [3] addons-trunk revision 4542 revid: qdp-launchpad@xxxxxxxxxxx-20110315154324-70oh0npusr2v41ko [4] https://code.launchpad.net/~openerp-dev/openobject-server/6.0-osv-mem-v610