I defined several custom objects in OpenERP, which are supposed to be used in a multi-company environment. Accordingly, each object defines a "company_id" field. This is all setup and working fine. Only, the UI just isn't very user friendly, since the user needs to explicitly choose the company the new object instance should belong to.
The company_id fields are set up in the following way:
...
'company_id' : fields.many2one('res.company', string='Company', selection=compute_company_select, required=True)
...
def compute_company_select(self, cr, uid, context):
c_proxy = self.pool.get('res.company')
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
search = [('id', 'child_of', user.company_id.id)]
if uid == SUPERUSER_ID:
search = [] #superuser can assign any company
ids = c_proxy.search(cr, uid, search, context=context)
return [(c.id, c.name) for c in c_proxy.browse(cr, uid, ids, context=context)]
I could also solve this by simply defining a domain expression in the field definition, using the search domain from the "compute_company_select" function. Except that the superuser couldn't assign all companies then, if he isn't configured to have access to all of them.
What I want to do now is pre-populate the company_id field with the users company, when he creates a new record. And this seems to be impossible. I have found several possibilities to pre-populate fields with either static values or values that have been defined in module-XML. But there seems to be no option to use a value that is only available during runtime, i.e. the current users record.
I am using OpenERP 6.1.