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 directly create a menu and submenu from the User interface of Odoo.
We can begin by constructing a menu using xml, or 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'll add an XML file to the module containing its definition.
1. Make an XML file to store the data records for the UI Views > patient.xml
2. Add a data file on the __manifest__.py and add the XML file in the data.
'data': ['views/view.xml'],
3. Add an action for the view.
<record id="Record_action" model="ir.actions.act_window">
<field name="name">Name of Record</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">model.record</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a new record!
</p>
</field>
</record>
name - Name of the action.
res model - The model in which the menu is created.
view mode – Various view modes. This mode must be defined by the preceding action.
view_id - The default view that must be loaded when the action is invoked is view id.
4. Add a menu item to the patient.xml file to visible to the users.
<menuitem id="menu_id"
name="Menu Name"
parent="parent_menu_root"
action="Record_action"
sequence="10"/>
id - id of the menu
name - Name of the menu which is to be displayed
sequence - Sequence in which menu is displayed
Action - We use the id of the window action we created in the previous step
Parent - This is the identifier for the parent menu item
5. Add a view to the file
<record id="record_view_form" model="ir.ui.view">
<field name="name">record.name.form</field>
<field name="model">record.model</field>
<field name="arch" type="xml">
<form>
<sheet>
<div class="oe_title">
<h1>
<field name="reference_no" readonly="1"/>
</h1>
</div>
<group>
<group>
<field name="name_id"/>
<field name="date"/>
<field name="age"/>
<field name="gender"/>
<field name="note"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
name: To identify the view. Add human readable title as a name.
model: Identify the target model.
arch: This is the view architecture, where its structure is actually defined.
6. Add a tree view to the file
<record id="record_view_tree" model="ir.ui.view">
<field name="name">record.name.tree</field>
<field name="model">record.model</field>
<field name="arch" type="xml">
<tree>
<field name="name_id"/>
<field name="age"/>
<field name="gender"/>
</tree>
</field>
</record>
7. Check on the UI.
Menu for patient details can be see here.
The image above shows the form view for the patient card.
The patient card tree view is depicted in the image above.
We can create a menu, action, and view in Odoo in this manner.
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.
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 demo.py
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 __init__.py 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 __manifest__.py 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!
</p>
</field>
</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"
parent="menu_patient_root"
action="record_action"
sequence="10"/>
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">record.name.form</field>
<field name="model">record.model</field>
<field name="arch" type="xml">
<form>
<sheet>
<div class="oe_title">
<h1>
<field name="reference_no" readonly="1"/>
</h1>
</div>
<group>
<group>
<field name="name_id"/>
<field name="date"/>
<field name="age"/>
<field name="gender"/>
<field name="note"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
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">record.name.tree</field>
<field name="model">record.model</field>
<field name="arch" type="xml">
<tree>
<field name="name_id"/>
<field name="age"/>
<field name="gender"/>
</tree>
</field>
</record>
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