Chapter 3 - Odoo 15 Development Book

Adding models

In Odoo, A model is a class that corresponds to a data relationship (table). It includes all of the necessary fields and behaviors for the data you'll be storing. In the vast majority of cases, each model is associated with a single database table.

There are mainly three types of models.

  • 1. Abstract model
  • 2. Transient model
  • 3. Models.


Model fields are defined as attributes on the model itself:

 from . import models, fields
                               class ExampleModel(models.Model):
	                          _name = "model name"
	                       Field = fields.Char(string="fields label")

Abstract Model

Main super-class for regular database-persisted Odoo models.

All Odoo models are created by inheriting from this class:

                            from . import models, fields
                            class AbstractModel(models.AbstractModel):
                            _name = "model name"
                           Field = fields.Char(string="field label")

Transient model

Model super-class for transient records that are intended to be only temporarily persistent and vacuum-cleaned on a regular basis.

The management of access rights has been simplified using a temporary paradigm. All users have the ability to create new records. However, they can only access the records that they have produced. All Transient Model records are accessible to the superuser without restriction.

                            from . import models, fields
                            class TransientModel(models.Transientmodel):
	                        _name = "model name"
	                       Field = fields.Char(string="field label")

Creating models

To create a new model, we need to add a python file describing it and then upgrade the module. The paths that are used are relative to our add-on module’s path.

1. Add a python file to the models named

                            from odoo import models, fields
                            class DemoClass(models.Model):
	                        _name = "demo.class"
	                        name = fields.Char("Name")
	                        date = fields.Date("Date")

2. Add a python initialization file to be loaded the models that are the file

 from . import demo

3. Edit the module’s python initialization file to have the models directory loaded by the module

 from . import models

4. Upgrade the odoo module from the apps menu in the user interface. We can check if the model is added. Activate the developer mode and go to General Settings > Technical > Database Structure> models search for our model “demo.class” on that

Adding Menus and Views

Odoo has some good ways to define menu, sub menu, views and actions. The main attraction is that a menu and sub menu can be created using both code and UI, making Odoo more user-friendly.

A user can create a menu and submenu directly from the UI.

We can start by creating a menu from xml. ie, from code For that, we can create a new module named hospital and also create a view directory on the hospital module.

To add a view we will add an XML file with its defenition to the module.

Create the XML file to add the data records describing the UI Views > patient.xml Add a data file on the and add the XML file in the data 'data': ['views/patient.xml'],

Add an action for the view

   <record id="record_action" model="ir.actions.act_window">
    <field name="name">Record Name</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">record.model</field>
    <field name="view_mode">tree,form</field>
    <field name="help" type="html">
        <p class="o_view_nocontent_smiling_face">
            Create a new record!

name - name of the action

res_model - model in which we create the menu.

view_mode - different view modes. This mode has to be define the above action.

view_id - default view that has to be load when the action is called.

Add a menu item to the patient. xml file to visible to the users

 <menuitem id="menu_id"
     	name="Menu Name"

id - id of the menu

name - name of the menu which is to be displayed

sequence - sequence in which menu is displayed

Action - action is the id of the window action we created on the above step

Parent - This is the identifier for the parent menu item

Add a view to the file

  <record id="record_view_form" model="ir.ui.view">
    <field name="name"></field>
    <field name="model">record.model</field>
    <field name="arch" type="xml">
                <div class="oe_title">
                        <field name="reference_no" readonly="1"/>
                        <field name="name_id"/>
                        <field name="date"/>
                        <field name="age"/>
                        <field name="gender"/>
                        <field name="note"/>

name: to identify the view. Add human readable title as a name

model: Identify the target model

arch: This is the view architecture, which is where the structure of the view is defined.

Add a tree view to the file.

  <record id="record_view_tree" model="ir.ui.view">
   <field name="name"></field>
   <field name="model">record.model</field>
   <field name="arch" type="xml">
       	<field name="name_id"/>
       	<field name="age"/>
       	<field name="gender"/>

Check on the UI

Menu for patient details can be see here.

Form view for patient card.

Patient card tree view.

In this way we can create a menu action and views in Odoo



Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message