Installing default data in Odoo module

Hi guys,

In this tutorial I will learn you how to install data in a module by default. This will mean that data is automatically inserted in the database whenever you would install this module. In some cases this is very handy because you need some default values inserted when the module is installed.

1. Adding a new XML file to the module

The first thing you need to do is create a new XML file in your custom module, for example demodata.xml, and to add the filename to __openerp__.py. An example of my __openerp__.py file:

Create default data

After you’ve added the location to the file (to __openerp__.py) you should create the data for the file. Open up your custom module, go in to the folder data and edit your XML file. The structure is always the same. You give the record a name (this can be anything, make sure its unique) and add the name of the model where you want data installed in. Then add a field for every field you have in the model which you want to fill with data. The following is an example that creates a record in the model ‘demo.default.data’ when you install the module:

As you can see by this record with demo data it will create a new record with a name named ‘Record #1’ and a description with the text ‘A description for record #1’. These are the fields that I’ve created in my custom model (models.py) like this:

When you would now install this module it will automatically install the records that are defined.
Example demo data

Want to see an example module where you can see how everything works? You can download/view it on Github!
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




5 thoughts on “Installing default data in Odoo module

  1. Joe says:

    I want to write code for default list coin in cash box. In odoo 8, it is available, but odoo 9 it is missing. Can you advice me how to do that.

    • Yenthe666 says:

      Hi Joe,

      The best advice I can give is to migrate the code from V8 to V9 and look at the source code, you’ll learn a lot from it! Implementation will probably be a bit different but it atleast gives you some good insight.

  2. OBINNA JUSTIN ASUZU says:

    Hi

    Thanks for this guide and many more you’ve posted, it really helps me alot.
    I am working on a custom module and I want to change setting values in the models: “sales.config.setting” and “stock.config.setting” whenever the module is installed. I tried using default data but it didn’t work

    • Yenthe666 says:

      Hi Obinna,

      This isn’t really a very easy thing to do. You could query to the database right away or add some extra functions that go off but you won’t be able to do this with default data.

Leave a Reply

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