1
votes

In my OpenERP application, I have a lambda that I am trying to understand (currency_id):

_defaults = {
       'display_type': True,
       'journal_ids': [],
       'target_move': False,
       'currency_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
   }

So far, I understand it is doing this:

  • start with table res_users, which has a company_id field
  • uses the browse command to access a connected record

The browse function has the prototype of: browse(cr, uid, ids, context=None). We are passing it the uid as the ids. Why are we passing the uid instead of ids?

The res_company table has the currency_id field.

Then I assume it is using the foreign keys to access it via OpenERP's ORM. How does the ORM know how to connect to the **res_company field?**

Similar question what is the reason of using _defaults and lambda in python for openerp development?

1

1 Answers

1
votes

You are correct: we do want to pass ids to browse. However, in this case uid is the id of the res.users object who is currently logged in. Thus,

self.pool.get('res.users').browse(cr, uid, uid, c)

returns the browse_record object corresponding to the logged in user. This user has a company associated with it (via company_id) which has a currency (currency_id), and we use that currency's id as the default currency for anything this user does.