Back to Top

Working with XML data in Odoo

Updated 1 October 2019

Odoo gives the developer several options  for working with data .

In this article, we will be looking at the XML data options in odoo.
We will study how to create , update and  delete the records using xml data.

 

Create DEMO DATA File

First of all you need to create a xml file(let’s say demo.xml)  Like:

 

Searching for an experienced
Odoo Company ?
Find out More
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
    </data>
</odoo>

Here noupdate is set to true for ensuring that on module update  this xml will not executed again and again.

It’s time to include your xml file in data or demo key of __mainifest__.py.

Create  Records

For creating the record of model  , create a <record/> tag and place the fields Like:

<record model="model_name" id="record_id">
    <field name="field_name">field_data</field>
</record>


<record model="product.product" id="product_webkul">
    <field name="name">Webkul Product</field>
</record>

Now your xml file will look like:

<odoo>
    <data noupdate="1">
        <record model="product.product" id="product_webkul">
            <field name="name">Webkul Product</field>
        </record>
    </data>
</odoo>

Modify  Records

Some time you  may required to update the previously created record by other module.
For this create the record with  id (module_name.record_id) in your module like:

<record model="model_name" id="module_name.record_id">
    <field name="field_name">new_field_data</field>
</record>

<record model="product.product" id="product.service_order_01">
    <field name="name">Demo Product</field>
</record>

NOTE: you must place the module_name in depends of __mainifest__.py

Delete  Records

In case you want to delete the record , use <delete/> tag  along with id or search   like:

<delete model="model_name"   search="[('id','=',ref('module_name.record_id'))]"/>
<delete model="model_name" id="module_name.record_id"/>
<delete model="product.product" search="[('id','=', ref('product.service_order_01'))]"/>
<delete model="product.product" id="product.service_delivery"/>

While using  search you can specify  domain  on fields also like

<delete model="product.product" search="[('name','=','demo'),('sale_ok','=',False)]"/>

NOTE:

You must also place the module_name in depends  keys of __mainifest__.py.

Hope  you enjoyed this post, I’d be very grateful if you’d write your opinions, comments and suggestions to keep the page updated and interesting.

You also like our post on calling methods from xml in odoo.

. . .

Leave a Comment

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


4 comments

  • jorgescalona
    • Anisha Bahukhandi (Moderator)
  • Rodolfo Álvarez García
    • Webkul Support
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content