External IDs and Namespaces
An External ID can be simply defined as a unique identifier used to specify XML records defined. They are also called as XML IDs.These are records of the model ir.model.data which are imported in the case of importing data in Odoo.
When we are importing data using XML IDs, it initially checks for the existence of a record with the same identifier. If there is an existing record with the XML ID, then the UPDATE process will be worked. Otherwise, the CREATE operation will be processed. Usually, XML IDs will be the combination of the record ID and the module name.
E.g., sale.mail_act_sale_upsell
We can update any XML record if the external ID is known. Consider the below as an example.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="base.main_company" model="res.company">
<field name="name">My Company (San Francisco)</field>
</record>
</data>
</odoo>
Here ID of the record is given as XML ID of the main company.Since there is a record with this XML ID, update operation will be performed, resulting in the name changing of the main company.The name field value will be updated to “My Company (San Francisco). There is an option to get the External ID of any records from the User Interface. After enabling debug mode, developer tool menu will be shown. There is an option to see the metadata of the record from here. External ID is shown inside the metadata.
Namespaced phrases are phrases that contain exactly one dot. When using the external ID for a record tag, Odoo first checks if it is namespaced. That is, if it contains exactly one dot. If it is not namespaced, then Odoo adds the module name as the namespace for the record.
Delete Data from XML
So far, we have discussed that we can create or update records using XML. There may be some cases where we need to delete existing records which are previously created at the time of any module installation. Delete tags (<delete>) are useful in such cases.
There are two methods to delete records from XML.
1.Using XML ID
2. Using Search Domain
Using XML ID
We are able to delete previously created records using the XML ID of it. For that, a delete tag is used. The syntax is given below.
<delete model="your model name" id="XML ID of the record"/>
Here, attribute id stands for the external id of the record that needs to be deleted and attribute model is the model of that data. We can only delete records in this way for those records which are previously created using the XML or records which have external ids. Consider an XML record for creating a new Product Category.
<record id="demo_category_1" model="product.category">
<field name="name">Category 1</field>
</record>
To delete this record, you have to follow the steps below.
<delete model = "product.category" id = "demo_category_1"/>
Odoo will search for a record inside the model product.category with the external id on running this code. If it is found, then the record will be deleted. If the record with this external id is not found, Odoo will raise an error.
Using Search Domain
This method helps in using a search domain for finding the record. We are able to pass a domain inside the search attribute of the delete tag. In this case, Odoo checks for matching records with the domain in the specified model while installing the module. The data will be deleted if records are found. In the first method using XML id, an error will be raised if no records are found. But here, no error will be raised if no matching records are found. The syntax is as follows
<delete model = "model_name" search="domain"/>
let us discuss the syntax with an example.
<delete model = "sale.order" search [(partner_id.name,'=','Azure Interior')]/>
By running the above code, Odoo will delete all Sale Orders of the partner whose name is Azure Interior. Commonly delete is rarely used in Odoo.