Sometimes, we need demo data to be loaded when we upgrade the module for the first time.

In this case, we have to create entries in xml.

Consider a module:

__init__.py

Import models

__openerp__.py

# -*- coding: utf-8 -*-

{

‘name’: “mymodule”,

‘author’: “My Company”,

‘website’: “http://www.yourcompany.com“,

‘category’: ‘Uncategorized’,

‘version’: ‘0.1’,

‘depends’: [‘base’],

‘data’: [

‘views.xml’,

   ‘demo’: [

          ‘demo.xml’, # file with demo data

],

}

 

models.py

from openerp import models, fields, api

class my_module(models.Model):

_name = ‘my.module’

name = fields.Char()

value = fields.Integer()

value2 = fields.Float(compute=”_value_pc”, store=True)

description = fields.Text()

@api.depends(‘value’)

def _value_pc(self):

self.value2 = float(self.value) / 100

 

 views.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<openerp>

<data>

<record model=”ir.ui.view” id=”my_module_list”>

<field name=”name”>my_module list</field>

<field name=”model”>my.module</field>

<field name=”arch” type=”xml”>

<tree>

<field name=”name”/>

<field name=”value”/>

<field name=”value2″/>

</tree>

</field>

</record>

<record model=”ir.ui.view” id=”my_module_form”>

<field name=”name”>my_module Form</field>

<field name=”model”>my.module</field>

<field name=”arch” type=”xml”>

<form string=”My Module”>

<field name=”name”/>

<field name=”value”/>

<field name=”value2″/>

</form>

</field>

</record>

<record model=”ir.actions.act_window” id=”action_my_module_id”>

<field name=”name”>my_module</field>

<field name=”res_model”>my.module</field>

<field name=”view_mode”>tree,form</field>

</record>

<menuitem name=”my_module” id=”menu_root”/>

<menuitem name=”Menu 1″ id=”my_module_menu_1″ parent=”menu_root” action=”action_my_module_id”/>

</data>

</openerp>

This is demo.xml which will have demo data loaded into it  Wherein Field name refers to Column name (In this example it is name,value)

<openerp>

<data noupdate=”1″>

<record id=”object0″ model=”my.module”>

<field name=”name”>Object 0</field>

<field name=”value”>0</field>

</record>

<record id=”object1″ model=”my.module”>

<field name=”name”>Object 1</field>

<field name=”value”>10</field>

</record>

<record id=”object2″ model=”my.module”>

<field name=”name”>Object 2</field>

<field name=”value”>20</field>

</record>

</data>

</openerp>

This demo data is loaded as soon as module is upgraded for the first time. Visit Nevpro.co.in to know further details.

Watch our videos on YouTube.