Creating Menus in Odoo with XML
Menus in Odoo are crucial for navigating through the application. This guide focuses on defining menus using XML in Odoo's custom modules.
1. Types of Menus in Odoo
- Root Menu: The top-level menu displayed in the navigation bar.
- Submenu: A menu nested under a root menu.
- Action Menu: A menu linked to an action, such as opening a specific view.
2. Basic Menu Structure
Here is a simple example of how menus are defined in XML:
<odoo>
<data>
<!-- Root Menu -->
<menuitem
id="menu_root_example"
name="Example Root Menu"
sequence="10" />
<!-- Submenu -->
<menuitem
id="menu_sub_example"
name="Example Submenu"
parent="menu_root_example"
sequence="20" />
<!-- Action Menu -->
<menuitem
id="menu_action_example"
name="Example Action"
parent="menu_sub_example"
action="action_example"
sequence="30" />
</data>
</odoo>
3. Explanation of Menu Elements
- id: A unique identifier for the menu, used to reference it elsewhere in the module.
- name: The name displayed in the user interface.
- parent: Specifies the parent menu (used for submenus).
- action: The action triggered when the menu is clicked (e.g., opening a list or form view).
- sequence: Determines the order of menus (lower numbers appear first).
4. Adding Actions to a Menu
Menus are typically linked to actions that define what the menu does. Actions are created using the <record>
element with the ir.actions.act_window
model.
Example: Action to Open a Model
<record id="action_example" model="ir.actions.act_window">
<field name="name">Example Action</field>
<field name="res_model">example.model</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Add records to the Example model!
</p>
</field>
</record>
Linking the Action to a Menu
<menuitem
id="menu_action_example"
name="Example Action"
parent="menu_sub_example"
action="action_example"
sequence="30" />
5. Complete Implementation Example
Let’s create a menu for a model named example.model. Below is the complete XML implementation:
<odoo>
<data>
<!-- Root Menu -->
<menuitem
id="menu_root_example"
name="Example Module"
sequence="10" />
<!-- Submenu -->
<menuitem
id="menu_sub_example"
name="Example Data"
parent="menu_root_example"
sequence="20" />
<!-- Action -->
<record id="action_example" model="ir.actions.act_window">
<field name="name">Example Data</field>
<field name="res_model">example.model</field>
<field name="view_mode">tree,form</field>
</record>
<!-- Menu with Action -->
<menuitem
id="menu_action_example"
name="View Data"
parent="menu_sub_example"
action="action_example"
sequence="30" />
</data>
</odoo>
6. Setting Menu Order
Use the sequence attribute to define the menu order. Menus with lower sequence numbers appear first.
7. Saving the File
The XML file defining the menu should be saved in the views directory of your module. Ensure it is added to the module’s manifest.py file.
Directory Structure
my_module/
│
├── __init__.py
├── __manifest__.py
└── views/
└── example_menu.xml
Adding to manifest.py
'data': [
'views/example_menu.xml',
],
8. Validation and Debugging
- Ensure that each menu ID is unique across the entire Odoo system.
- Check Odoo logs for errors if the menu does not appear.
- If a menu is not visible, verify that the user has the necessary access rights to the linked model.
By following this guide, you can create menus in Odoo using XML effortlessly. Happy coding! 🚀