官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 什邡市| 新竹县| 乐清市| 汤原县| 博野县| 塔城市| 新兴县| 涟水县| 合水县| 临海市| 新宾| 九江市| 平乡县| 莱州市| 桂东县| 万全县| 泗水县| 中卫市| 齐河县| 汝城县| 昆明市| 大厂| 宣武区| 山阴县| 肇东市| 牡丹江市| 将乐县| 增城市| 张家界市| 东乌珠穆沁旗| 资中县| 河源市| 安溪县| 买车| 区。| 克拉玛依市| 霍州市| 喀喇| 九江市| 五台县| 曲靖市|