Hello Odooers & Developers! Welcome to this Odoo technical article, where we will show you how to create menus and actions in Odoo. Menus and actions are essential for navigating and managing different views within the system, making it easier to access and organize data.
What You Will Learn in This Guide:
- What are Menus and Actions in Odoo?
- How to define an Action using XML
- How to create a Menu Item
- How to link the Menu to an Action
What are Menus and Actions in Odoo?
In Odoo, menus are navigation elements that help users access different views, while actions define what happens when a user clicks a menu item. Actions typically link to views such as tree (list), form, or kanban views.
To create a menu and action in Odoo, follow these steps:
- Define the Action: Actions determine what happens when a menu is clicked.
- Create the Menu Item: Menus provide access points to different actions.
- Link the Menu to the Action: This step ensures that clicking the menu executes the intended action.
Let's go into detail about each step.
Step 1: Define the Action
Actions in Odoo tell the system what to display when a user clicks on a menu item. Actions can be of different types, such as:
- Window Actions (opens a view for a model)
- Server Actions (executes Python code)
- Report Actions (generates a report)
For this tutorial, we will create a Window Action to display a list of records from a model.
Let’s understand it by custom module example.
Example: Define a Window Action
Create an XML file inside your module (e.g., views/lessons_views.xml) and add the following action:
XML
<odoo>
<record id="lession_menu_action" model="ir.actions.act_window">
<field name="name">Lessons</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">odoo.lessons</field>
<field name="view_mode">tree,form</field>
</record>
</odoo>
Explanation:
- id="lession_menu_action": Unique identifier for this action.
- model="ir.actions.act_window": Indicates it's a window action.
- <field name="name">Lessons</field>: The name displayed in the UI.
- <field name="res_model">odoo.lessons</field>: The model to be displayed.
- <field name="view_mode">tree,form</field>: Specifies the views available (list and form views).
Step 2: Create the Menu Item
A menu item provides navigation to different actions within Odoo. You must define at least one root menu and optionally create submenus.
Example: Create a Learn Odoo Menu
Add the following XML code to views/menu.xml:
XML
<odoo>
<menuitem name="Learn Odoo" id="learn_odoo_main_menu_id" sequence="1"/>
</odoo>
Explanation:
- id="learn_odoo_main_menu_id": Unique ID for the menu.
- name="Learn Odoo": The name displayed in the UI.
- sequence="1": Determines the order in which the menu appears.
Step 3: Link the Menu to the Action
You can create multiple levels of menus in Odoo. Here’s an example where we define a parent menu, a submenu, and link it to an action.
Example: Nested Menus with an Action
Add the following XML code inside views/lessons_views.xml:
XML
<odoo>
<!-- Parent Menu -->
<menuitem name="Lessons" id="lesson_menu_id" sequence="1" parent="learn_odoo_main_menu_id"/>
<!-- Submenu linked to an action -->
<menuitem name="Lessons 2" id="lesson_menu2_id" sequence="1" parent="lesson_menu_id" action="lesson_menu_action"/>
</odoo>
Explanation:
- id="lesson_menu_id": Unique ID for the main menu (parent).
- name="Lessons": The display name of the menu.
- parent="learn_odoo_main_menu_id": Defines the main parent menu under which this menu appears.
- sequence="1": Determines the order in which the menu appears.
- id="lesson_menu2_id": Unique ID for the submenu.
- name="Lessons 2": The display name of the submenu.
- parent="lesson_menu_id": Defines Lessons as the parent of Lessons 2.
- action="lesson_menu_action": Links the submenu to an action, which defines what happens when clicked.
Step 4: Load the Files in the Manifest
For Odoo to recognize these menus and actions, add the XML files to your module's __manifest__.py file:
.py
{
'name': 'Learn Odoo',
'version': '1.0',
'category': 'Custom',
'summary': 'Custom module with menu and actions',
'depends': ['base'],
'data': [
'views/menu.xml',
'views/lessons_views.xml',
],
'installable': True,
'application': True,
}
Explanation:
- 'data': ['views/menu.xml', 'views/lessons_views.xml']: Ensures these files are loaded into the database.
- The Lessons menu is created under learn_odoo_main_menu_id.
- A submenu Lessons 2 is created under Lessons and linked to an action.
Step 5: Install and Test the Module
Once the module is ready, install it in Odoo:
- Restart the Odoo server.
odoo-bin -c odoo.conf -u my_module - Navigate to Apps > Update Apps List and install your module.
- Go to Learn Odoo> Lessons 2 and verify that the list view is displayed.
Conclusion
Creating menus and actions in Odoo is a fundamental aspect of module development. By following these steps:
- You defined an Action to display model records.
- You created a Menu Item for navigation.
- You linked the Menu to the Action.
- You updated the Manifest and installed the module.
Now, you can expand on this by adding more models, customizing views, and integrating additional functionalities. Mastering menu and action creation in Odoo will help you develop structured and user-friendly applications.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
If you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.