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
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.