What is External ID in Odoo ?

When you work with odoo, have you ever had any questions about what is an External ID ? Like when you are going to export some data, for example the customer as shown below ?

Or when you edit a form.

What exactly is an External ID ? And what does it do ?

Basically an External ID is a combination of several fields from the ir.model.data model, which is used to store the information of some data that stored in other table in the database. The information that the ir.model.data model stored is about in what module the data is created, in what table or model, and its primary key. Please take a look at the ir.model.data model source code at odoo github page.

For example, suppose we have an exported xlsx / csv file as shown below.

Usually an External ID is a combination of the module field and the name field of the ir.model.data model. By reading an External ID we can find out that the data is created manually by the user via the odoo User Interface, or created automatically when the user installs / upgrades a module.

For example, the External ID of the first line of exported data in the image above is __export __.res_partner_44_df40f25d. There is no module with the name of __export__ in odoo, so we can be sure that this data is created manually by the user via the odoo user interface.

On the second line, the External ID is base.res_partner_12. There is a module named base in odoo, so we can be sure that this data is created automatically, when the base module is installed / upgraded by the user.

This means that this data is written in one of the files in the base module. If you don’t believe me, please take a look at odoo github page.

If we know some External ID and with the help of a little SQL, we can extract some information about some data. For example as shown in the image below.

From the image above, we can see that the base.res_partner_12 External ID is linked to the res.partner model with the res_id (primary key) of 14. Ok , now let’s take a look at the data in our res.partner model.

You can see that the data is same as the exported file.

Or if you lazy enough to write some SQL commands, you can use python, as in the example below.

# get the information of the model and related res_id (primary key)
data = self.env['ir.model.data'].xmlid_to_res_model_res_id('base.res_partner_12')

if data:
    # get the information of the related model
    print(data[0])

    # get the information of the related res_id (primary key)
    print(data[1])


# get the information of the actual data
# for example if we already know that the related model is res.partner
# we can grab the needed information , like the partner name and the partner country

partner = self.env['ir.model.data'].xmlid_to_object('base.res_partner_12')
if partner:
    print(partner.name)
    print(partner.country_id.name)

I got a feedback from Twitter user that odoo has a more easy way to access object with External ID. Thank you @ray_odoo for your suggestion. Here is how to get the partner name and its country with base.res_partner_12 External ID above in a more easy way.

partner = self.env.ref('base.res_partner_12')
if partner:
    print(partner.name)
    print(partner.country_id.name)

By knowing an External ID we can change the already stored data via other module or addon, for example, we can create a custom module or addon. This way is commonly used to override or inherit a view. So that when we install or upgrade a custom module or addon, the data that we want can be changed automatically, the user doesn’t need to change it via the user interface. For example, the code below is shows how to change the customer’s name with External ID of base.res_partner_12 above with a custom module.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="base.res_partner_12" model="res.partner">
        <field name="name">Azure Exterior</field>
    </record>
</odoo>

This is the appearance of the customer after the custom module with the code above has been installed / upgraded. Look, the name has been changed.

When we write an External ID in the xml file to create a new record, make sure that in one module / addon there is no record with the same External ID name, even though it is in different files. It is also can not contain spaces. Try to use all lowercase letters, and each word is separated by an underscore. Module / addon name may not be included. Because it will automatically be included by odoo. For example at this odoo source code the base module / addon name is not written.

But if we want to override or to change the data from another module in xml file, we must write the External ID completely with the module name. Likewise, if we write External ID in the python file.

Related Article

Leave a Reply

Your email address will not be published. Required fields are marked *