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

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.

主站蜘蛛池模板: 罗源县| 松桃| 海淀区| 拉萨市| 米脂县| 云安县| 济宁市| 塔河县| 汉阴县| 高安市| 三穗县| 花莲县| 兴仁县| 铁岭市| 新龙县| 开鲁县| 太和县| 昌吉市| 涞源县| 浦东新区| 咸丰县| 临夏市| 怀仁县| 奉化市| 北碚区| 方城县| 惠东县| 黎平县| 会东县| 马关县| 屯昌县| 北海市| 浮梁县| 安福县| 长宁区| 香港 | 文安县| 同江市| 赞皇县| 大冶市| 淮滨县|