Adding menu items
Now that we have the places to store our data, we want to have them available on the user interface. The first thing to do should be to add the corresponding menu options.
Create the views/todo_menu.xml file to define a menu item and the action performed by it:
<?xml version="1.0"?> <odoo> <!-- Action to open To-do Task list --> <act_window id="action_todo_task" name="To-do Task" res_model="todo.task" view_mode="tree,form"
/> <!-- Menu item to open To-do Task list --> <menuitem id="menu_todo_task" name="Todos" action="action_todo_task"
/> </odoo>
The user interface, including menu options and actions, is stored in database tables. The XML file is a data file used to load those definitions into the database when the addon module is installed or upgraded. The preceding code is an Odoo data file, describing two records to add to Odoo:
- The <act_window> element defines a client-side window action that will open the todo.task model with the tree and form views enabled, in that order
- The <menuitem> defines a top menu item calling the action_todo_task action, which was defined before
Both elements include an id attribute. This id attribute, called an XML ID, is very important; it is used to uniquely identify each data element inside the module, and is the method other elements can use to reference it. In this case, the <menuitem> element needs to reference the action to process, and so needs to use the <act_window> XML ID for that. XML IDs are discussed in greater detail in Chapter 5, Import, Export, and Module Data.
Our module does not yet know about this new XML data file. For this to happen, it needs to be declared in the __manifest__.py file, using the data attribute. It is a list of the data files to be loaded by the module upon installation or upgrade.
Now add this attribute to the manifest's dictionary:
'data': ['views/todo_menu.xml'],
We need to upgrade the module again for these changes to take effect. Go to the Todos top menu and you should see our new menu option available. Clicking on it displays a basic list view, and we will have available an automatically generated form view:
Even though we haven't defined our user interface view, the automatically generated list and form views are functional, and allow us to start editing data right away.