Adding menu entries

Now that we have a model to store our data, let's make it available on the user interface.

All we need to do is to add a menu option to open the To-do Task model so that it can be used. This is done using an XML file. Just as in the case of models, some people consider it good practice to keep the view definitions inside a views subdirectory.

We will create a new todo_view.xml data file in the module's top directory, and it will declare a menu item and the action performed by it:

<?xml version="1.0"?>
<openerp>
  <data>

    <!-- 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="To-Do Tasks"
      parent="mail.mail_feeds"
      sequence="20"
      action="action_todo_task" />

  </data>
</openerp>

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 module is installed or upgraded. This is an Odoo data file, describing two records to add to Odoo:

  • The <act_window> element defines a client-side Window Action to open the todo.task model defined in the Python file, with the tree and form views enabled, in that order.
  • The <menuitem> defines a menu item under the Messaging menu (identified by mail.mail_feeds), calling the action_todo_task action, which was defined before. The sequence lets us set the order of the menu options.

Now we need to tell the module to use the new XML data file. That is done in the __openerp__.py file using the data attribute. It defines the list of files to be loaded by the module. Add this attribute to the descriptor's dictionary:

    'data': ['todo_view.xml'],

Now we need to upgrade the module again for these changes to take effect. Go to the Messaging menu and you should see our new menu option available.

Adding menu entries

Clicking on it will open an automatically generated form for our model, allowing us to add and edit records.

Views should be defined for models to be exposed to the users, but Odoo is nice enough to do that automatically if we don't, so we can work with our model right away, without having any form or list views defined yet.

So far, so good! Let's improve our user interface now. Try the gradual improvements as shown in the next sections, doing frequent module upgrades, and don't be afraid to experiment.

Tip

In case an upgrade fails because of an XML error, don't panic! Comment out the last edited XML portions, or remove the XML file from __openerp__.py, and repeat the upgrade. The server should start correctly. Now read the error message in the server log carefully—it should point you to where the problem is.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset