Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Database Id from an XML Id

Tags:

openerp

The osv.osv provides a get_xml_id method to find the XML Id for the provided Database Id. What is the best way to do the opposite?

Knowing the XML Id (it was defined in the data loading file), how can I get the corresponding Database Id, so that I can refer to it in tour Python code?

like image 411
Daniel Reis Avatar asked Dec 29 '11 10:12

Daniel Reis


2 Answers

The ir.model.data model also has a get_object() method returning a browsable record given a model name and an xml_id.

So, another solution could be:

    m  = self.pool.get('ir.model.data')
    id = m.get_object(cr, uid, 'base', 'user_root').id
like image 118
Daniel Reis Avatar answered Nov 15 '22 22:11

Daniel Reis


The ir_model_data object has a _get_id() method that does what you're looking for. You can see it in use in the res_users._get_admin_id() method:

def _get_admin_id(self, cr):
    if self.__admin_ids.get(cr.dbname) is None:
        ir_model_data_obj = self.pool.get('ir.model.data')
        mdid = ir_model_data_obj._get_id(cr, 1, 'base', 'user_root')
        self.__admin_ids[cr.dbname] = ir_model_data_obj.read(cr, 1, [mdid], ['res_id'])[0]['res_id']
    return self.__admin_ids[cr.dbname]
like image 7
Don Kirkby Avatar answered Nov 15 '22 20:11

Don Kirkby