Creating Menus and Window Actions in Odoo 17

Abid Patel
30-Aug-2024 Updated : 30-Aug-2024

Learn how to create menus and window actions in Odoo 17 with this detailed guide. Customize your Odoo interface by organizing menus and defining actions for seamless navigation.

Creating Menus and Window Actions in Odoo 17

Odoo 17, the latest iteration of the powerful open-source ERP platform, offers developers a wide array of tools to build and customize applications. Among these tools, the ability to create custom menus and window actions is essential for defining the user interface and improving the usability of the applications. Menus allow users to navigate between different parts of the application, while window actions define what happens when a menu item is clicked.

In this comprehensive guide, we will walk you through the process of creating menus and window actions in Odoo 17. By the end of this tutorial, you’ll be able to structure your Odoo application’s navigation effectively, making it easier for users to interact with your custom modules.

Step-by-Step Guide How to Creating Menus and Window Actions in Odoo:

1. Understanding Menus and Window Actions in Odoo 
2. Setting Up Your Odoo Development Environment 
3. Creating a Custom Module Structure 
4. Defining Models for Your Menus 
5. Creating Menus in Odoo 17 
6. Creating Window Actions in Odoo 17 
7. Adding Views for Your Model 
8. Applying Security Rules 
9. Testing and Debugging Your Menus and Actions

1. Understanding Menus and Window Actions in Odoo

Before diving into the implementation, it’s crucial to understand what menus and window actions are and how they function within Odoo.

Menus:

- Main Menus: The top-level navigation elements that appear in Odoo’s main navigation bar.

- Submenus: Nested under the main menus, these provide more specific navigation options.

- Menu items: The actual clickable items that users interact with. These items can lead to views, forms, or other actions.

Window Actions:

- Window Actions: These are specific actions triggered by clicking a menu item. A window action typically opens a particular view, such as a list, form, or kanban view, associated with a model.

Relationship Between Menus and Window Actions:

- Menus are the user interface elements that direct users to different parts of the application.

- Window actions define what happens when a menu item is clicked, such as opening a view or executing a function.

2. Setting Up Your Odoo Development Environment

To create menus and window actions, ensure that your Odoo development environment is correctly set up. Here’s a quick overview:

- Install Odoo 17: Make sure you have Odoo 17 installed. You can run it locally or on a server.

- Create a Custom Module: We’ll be adding menus and window actions to a custom module. If you don’t already have one, create it using Odoo’s scaffolding command.

- Prepare Your Editor: Use a text editor like Visual Studio Code or PyCharm, which supports Python and XML, the primary languages used in Odoo development.

3. Creating a Custom Module Structure

Before we start creating menus and window actions, let's set up the basic structure of a custom module.

1. Scaffold a New Module: Use Odoo's scaffolding command to create a new module. Run the following command in your terminal:

' odoo-bin scaffold custom_menu_module addons_path/ '

This command creates a module with the necessary directories and files.

2. Understanding the Module Structure:

- models: This directory will contain Python files defining the data models.

- views: This directory is where you will define the XML files for your menus, views, and actions.

- security: Access control and security rules are defined here.

- init.py and manifest.py: These files initialize the module and define its metadata.

4. Defining Models for Your Menu

Before creating menus, you need a model to associate with the window actions. Let’s define a simple model in your custom module.

1. Create a Python File for the Model: U Navigate to the 'models' directory and create a file named 'menu_model.py'.

' custom_menu_module/models/menu_model.py '

2. Define a Simple Model: Open menu_model.py in your editor and define a basic model.

menu_model.py

from odoo import models, fields

class MenuModel(models.Model):
    _name = 'menu.model'
    _description = 'Menu Model'

    name = fields.Char(string='Name', required=True)
    description = fields.Text(string='Description')

In this example:

The model 'menu.model' is defined with fields 'name' and 'description'.

This model will be used in the views and actions linked to the menu.

3. Update ' __init__.py ' Ensure that the model is imported in the module’s '__init__.py' file.

__init__.py

from . import menu_model

5. Creating Menus in Odoo 17

With the model in place, we can now create menus that link to this model.

1. Define the Menu Structure: Menus in Odoo are defined in XML files, typically within the 'views/' directory. Create a new XML file named 'menu_views.xml'.

' custom_menu_module/views/menu_views.xml '

2. Create Main Menu and Submenus: Open menu_views.xml in your editor and define the menu structure.

menu_views.xml

<odoo>
    <menuitem id="main_menu_custom" name="Custom Menu" sequence="10" />

    <menuitem id="submenu_custom" name="Submenu" parent="main_menu_custom" sequence="10" />

    <menuitem id="menuitem_menu_model" name="Menu Model" parent="submenu_custom"
              action="action_menu_model" sequence="10"/>
</odoo>

Explanation:

- main_menu_custom: This is the main menu that appears in the Odoo navigation bar.

- submenu_custom: A submenu nested under the main menu.

- menuitem_menu_model: A specific menu item under the submenu that will trigger a window action.

3. Add the Menu Definition to the Manifest: To ensure Odoo loads this menu, add the XML file to the module’s manifest file '(__manifest__.py)'.

menu_views.xml

'data': [
    'views/menu_views.xml',
],

6. Creating Window Actions in Odoo 17

Now that we have menus in place, let’s define the window actions that will be triggered when these menus are clicked.

1. Define a Window Action: Window actions are also defined in XML. You can include the action in the same 'menu_views.xml' file.

menu_views.xml

<odoo>
    <!-- Window Action -->
    <record id="action_menu_model" model="ir.actions.act_window">
        <field name="name">Menu Model</field>
        <field name="res_model">menu.model</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first record
            </p>
        </field>
    </record>
</odoo>

Explanation:

- action_menu_model: This is the ID of the window action.

- res_model: The model associated with the action, which in this case is 'menu.model'.

- view_mode: Specifies the views to open when the action is triggered. 'tree,form' opens a list view first, then a form view on selecting a record.

2. Link the Action to the Menu Item: In the previous menu definition, we linked the 'menuitem_menu_model' to 'action_menu_model'. This link ensures that clicking the menu item triggers the window action.

7. Adding Views for Your Model

For the window action to display data, you need to define views for the model.

1. Create a Tree View: A tree view displays multiple records in a list format. Define a tree view in the 'menu_views.xml' file.

menu_views.xml

<record id="view_tree_menu_model" model="ir.ui.view">
    <field name="name">menu.model.tree</field>
    <field name="model">menu.model</field>
    <field name="arch" type="xml">
        <tree>
            <field name="name"/>
            <field name="description"/>
        </tree>
    </field>
</record>

2. Create a Form View: A form view is used to view or edit a single record. Define a form view in the same file.

menu_views.xml

<record id="view_form_menu_model" model="ir.ui.view">
    <field name="name">menu.model.form</field>
    <field name="model">menu.model</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="description"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

3. Link Views to the Window Action:Modify the window action to reference these views.

menu_views.xml

<field name="view_id" ref="view_tree_menu_model"/>
<field name="view_mode">tree,form</field>
<field name="view_ids" eval="[(5, 0, 0), (0, 0, {'view_mode': 'form', 'view_id': ref('view_form_menu_model')})]"/>

This setup ensures that when users access the menu item, they see the list (tree view) first and can open individual records in the form view.

8. Applying Security Rules

To control access to your menus and actions, define security rules in the 'security/' directory.

1. Create an Access Control List (ACL): Create a new XML file, 'ir.model.access.csv', in the 'security/' directory to define access rights.

ir.model.access.csv

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_menu_model_user,access.menu.model.user,model_menu_model,base.group_user,1,1,1,1

Explanation:

- access_menu_model_user: The ID of the access rule.

- model_menu_model: The model this rule applies to.

- base.group_user: The user group that has access to this model.

- 1, 1, 1, 1: Grants read, write, create, and delete permissions to the group.

2. Add the ACL to the Manifest: Update the manifest to include the security file.

__manifest__.py

'security': [
    'security/ir.model.access.csv',
],

9. Testing and Debugging Your Menus and Actions

With everything set up, it’s time to test your module.

1. Update the App List: Go to the Odoo interface and update the app list to recognize your new module.

2. Install the Module: Locate your module in the Apps menu and install it.

3. Test the Menus: 
Navigate through the menus you created to ensure they are correctly structured.
Click the menu items to trigger the window actions and verify that the associated views are displayed as expected.

4. Debugging: 
If something doesn’t work as expected, check the Odoo logs (odoo.log) for errors.
Verify that the model, views, menus, and actions are correctly defined in the XML files.

10. Conclusion

Creating menus and window actions in Odoo 17 is a fundamental skill that allows you to customize the user interface to meet specific business needs. By following this guide, you should now be comfortable with setting up custom menus, defining window actions, and linking them to views in your Odoo application.

This process not only enhances the usability of your custom modules but also makes your application more intuitive and user-friendly. As you continue to develop in Odoo, you’ll find that mastering these elements opens up a world of possibilities for building powerful, tailored solutions.

Whether you’re creating a simple application or a complex enterprise solution, the ability to structure menus and actions effectively is key to delivering a seamless user experience. Keep experimenting, and don’t hesitate to dive deeper into Odoo’s extensive documentation and community resources to expand your knowledge even further.

Happy coding!

Make a Comment

Your email address will not be published. Required fields are marked *