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?
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With