Display records#

Having records in the database is nice but we want the user to manage this records through the user interface.

In order to denote that a model can be displayed in the interface, you have to inherit from ModelView:

from trytond.model import ModelSQL, ModelView
...

class Opportunity(ModelSQL, ModelView):
    ...

When you inherit from ModelView, your model gains the methods required to display the data on Tryton clients. Those methods allow to retrieve the fields and the definition of the views used by a model, to apply attributes on view elements and they also provide all the machinery for on_change and on_change_with.

Tryton Views#

In Tryton data can be displayed using different kind of views. The available view types and it’s attributes are listed on the Views topic.

Tryton views are usual Tryton records that are persisted into the database. This design choice means that views are extendable and that you can use the traditional Tryton concepts when interacting with them.

Define views#

Views are defined in XML files and they contain one XML tag for each element displayed in the view. The root tag of the view defines the view type. An example view for our opportunity module will be as follows:

Here is the content of the form view of opportunity in view/opportunity_form.xml:

<form>
   <label name="party"/>
   <field name="party"/>
   <label name="description"/>
   <field name="description"/>
   <label name="start_date"/>
   <field name="start_date"/>
   <label name="end_date"/>
   <field name="end_date"/>
   <separator name="comment" colspan="4"/>
   <field name="comment" colspan="4"/>
</form>

And here is the content of the list view in view/opportunity_list.xml:

<tree>
   <field name="party"/>
   <field name="description"/>
   <field name="start_date"/>
   <field name="end_date"/>
</tree>

The value of the name attribute for field and label tags is the name of the field attribute of the model. Each XML tag can contain different attributes to customize how the widgets are displayed in the views. The full reference can be found on the Views section.

Once a views is defined it must be registered on the Tryton database in order to make the server know about them. In order to do so with should register it on a XML file specifying the following information:

model

The name of the model of the view

type

Possible values are: tree, form, calendar, graph, board

name

The name of the XML file (without extension) in the view folder which contains the view definition

Here is the content of the opportunity.xml file:

<tryton>
   <data>
      <record model="ir.ui.view" id="opportunity_view_form">
         <field name="model">training.opportunity</field>
         <field name="type">form</field>
         <field name="name">opportunity_form</field>
      </record>

      <record model="ir.ui.view" id="opportunity_view_list">
         <field name="model">training.opportunity</field>
         <field name="type">tree</field>
         <field name="name">opportunity_list</field>
      </record>
   </data>
</tryton>

Now we have to declare the XML data file in the tryton.cfg file:

[tryton]
...
xml:
   opportunity.xml

Create menu entry#

In order to show our models on the user menu we need an Window Action and a menu entry.

An action window is used to relate one or more views, usually a list and a form view.

Here is the definition of the opportunities action to append into opportunity.xml:

<tryton>
   <data>
      ...
      <record model="ir.action.act_window" id="act_opportunity_form">
         <field name="name">Opportunities</field>
         <field name="res_model">training.opportunity</field>
      </record>
      <record model="ir.action.act_window.view" id="act_opportunity_form_view1">
         <field name="sequence" eval="10"/>
         <field name="view" ref="opportunity_view_list"/>
         <field name="act_window" ref="act_opportunity_form"/>
      </record>
      <record model="ir.action.act_window.view" id="act_opportunity_form_view2">
         <field name="sequence" eval="20"/>
         <field name="view" ref="opportunity_view_form"/>
         <field name="act_window" ref="act_opportunity_form"/>
      </record>
   </data>
</tryton>

A menu entry is created using the special menuitem XML tag which accepts the following values:

id

Required XML identifier to refer this menu_item from other records.

sequence

Used to define the order of the menus.

action

The action to execute when clicking the menu.

name

The string that will be shown on the menu. If no name is entered and an action is set, the action name will be used.

parent

The parent menu when creating a sub-menu.

Lets add a menu entry to the opportunity.xml file with:

<tryton>
   <data>
      ...
      <menuitem
         name="Opportunities"
         sequence="50"
         id="menu_opportunity"/>
      <menuitem
         parent="menu_opportunity"
         action="act_opportunity_form"
         sequence="10"
         id="menu_opportunity_form"/>
   </data>
</tryton>

Update database#

As we have defined new XML records, we need to update the database with:

$ trytond-admin -d test --all

And restart the server and reconnect with the client to see the new menu entries:

$ trytond

Let’s continue with setting default values.