Introduction about Odoo menus

Recently, I learned about odoo here. This site teaches me how to create a simple module. I practiced with the Open Academy module. I followed it and now I will show you my results about Odoo menus.

To create a menu in Odoo, you need to do some steps in your xml file. And after that, add your xml file to the data list of manifest.py file.

For example, I created a xml file named openacademy/views/opencademy.xml. This file contains my menu codes.

After I created opencademy.xml file, I add it to the data list of openacademy/manifest.py

'data': [
'security/ir.model.access.csv',
'views/openacademy.xml',
],

1. Introduce a root menu

An important component of the menu is the root menu. The root menu is shown in 2 positions:

  • The top left of the screen.

  • The main dropdown menu of Odoo

To create a root menu, you need to following the structure:

<menuitem id="main_openacademy_menu" name="Open Academy" />

Remember that the root menu has no parent.

From the root menu, create 2 children menus. It will show at the right of the root menu. Do it follow the structure:

<menuitem id="openacademy_menu" name="Open Academy" parent="main_openacademy_menu" />
<menuitem id="configuration_menu" name="Configuration" parent="main_openacademy_menu"/>

In this example, Open Academy and Configuration are children of the root menu.

2. Introduce a second root menu with tree views default

First, you need to create a model if you want to show your view. Some fields you show in your view need to declare on the model.

With my example, I do it in openacademy/models/model.py

There are some fields I need to prepare before showing them in my views.

class Course(models.Model):
_name = 'openacademy.course'
_description = "OpenAcademy Courses"
name = fields.Char(string="Title", required=True)
description = fields.Text()

But, you can still create tree view with two steps following:

  • Step 1: Create an action window res to tree view

  • Step 2: Create the menu to call action

Remember, the action must be declared before its corresponding menu in the XML file.

Data files are executed sequentially, the action’s id must be present in the database before the menu can be created.

Back to my example, click to the session menu and show the list session with a tree view.

  • Step 1: Create an action with id course_list_action, reference to module name openacademy.course and call view_mode is tree

    <record model="ir.actions.act_window" id="course_list_action">
         <field name="name">Courses</field>
         <field name="res_model">openacademy.course</field>
         <field name="view_mode">tree,form</field>
         <field name="help" type="html">
             <p class="o_view_nocontent">Create the first course</p>    
          </field>
    </record>
    
  • Step 2: Create menu item to call to course_list_action action

    <menuitem id="courses_menu" name="Courses" parent="openacademy_menu" action="course_list_action" />
    

Note, field name “view_mode”, you need to call tree mode for this action.

3. Introduce a third root menu with default kanban views.

In my example, create a menu named Contact (parent is Configuration). With this menu, I want to show the default kanban view that contains the information of the Partner model.

I follow steps:

  • Step 1: Defined an action has kanban mode with id contact_list_action

  • Step 2: Defined a item menu to call action contract_list_action

    <record model="ir.actions.act_window" id="contact_list_action">
    <field name="name">Contacts</field>
    <field name="res_model">res.partner</field>
    <field name="view_mode">kanban,tree,form</field>
    </record>
    
    <menuitem id="configuration_menu" name="Configuration" parent="main_openacademy_menu"/>
    <menuitem id="contact_menu" name="Contacts" parent="configuration_menu" action="contact_list_action"/>
1 Like

Pls take some screenshot have some visualized samples

1 Like

Phần code block toàn dính hai dấu nháy đơn chị @luna nha. Chị chẹc lại chỗ này nha

1 Like

hôm qua em có để toàn ảnh screenshot nhưng theo góp ý của walter em đã xóa hết đi ạ. Để tí em thêm vào comment cho phong phú :smiley:

uh hôm qua chỉnh định dạng code mãi nó không lên, thế là thay thử 2 dấu nháy đơn thì nó lên =)). Giờ chỉnh lại luôn chỗ đó đây

Some screenshots of my example

1. Root menu

Sub menu

2. Show tree view when click to menu

3. Show kanban view when click menu

Lúc code thì đúng là kiểu 1-2 cái dấu nháy cũng làm liêu xiêu cả buổi ý chị nhỉ :joy:

hôm trước chị vừa bị một cái bug vì một dấu khoảng trắng xong Vinnie ạ =))
Dấu nháy ở trên bài viết là vì chị chọn markdown định dạng code không được nên chị đổi type cho nó hợp lệ để đưa vào bài viết đó