I know I can find the existing external xml id of a record searching the ir.model.data model, but how can I force the creation of an xml id for a record that still doesn't have one by python code ?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
Hi Alessandro,
Alternatively if you'd like to stay closer to the Odoo standard without duplicating code or logic you could use the function "export_data" which triggers " _export_rows" that always generates XML ids to the default core standard. A sample line of code:
your_record.export_data(['id'])
If you directly want to catch the external ID from the function you can also capture it as it is returned in a list within a list in the datas key:
xml_id = your_record.export_data(['id']).get('datas')[0][0]
Alternatively, if you already know that an external ID is created, or want to check if it already exists, you could do so through the get_metadata function from Odoo:
xml_id = your_record.get_metadata()[0].get('xmlid')
Hi,
To generate the external id for the record from the python code, you can use below code,
def __export_xml_id(self):
""" Return a valid xml_id for the record ``self``. """
if not self._is_an_ordinary_table():
raise Exception(
"You can not export the column ID of model %s, because the "
"table %s is not an ordinary table."
% (self._name, self._table))
ir_model_data = self.sudo().env['ir.model.data']
data = ir_model_data.search([('model', '=', self._name), ('res_id', '=', self.id)])
if data:
if data[0].module:
return '%s.%s' % (data[0].module, data[0].name)
else:
return data[0].name
else:
postfix = 0
name = '%s_%s' % (self._table, self.id)
while ir_model_data.search([('module', '=', '__export__'), ('name', '=', name)]):
postfix += 1
name = '%s_%s_%s' % (self._table, self.id, postfix)
ir_model_data.create({
'model': self._name,
'res_id': self.id,
'module': '__export__',
'name': name,
})
return '__export__.' + name
Without python code, if you need to generate external id for record from the user interface, select the record and just export it, once we export the record odoo will set the external id for the record if it doesn't exist.
Thanks
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up