Define model#
The models are the base objects of a module to store and
display data.
The ModelSQL
is the base class that implements the
persistence in the SQL database.
The ModelView
is the base class that implements the
view layer.
And of course, a model would be useless without its fields.
Let’s start with a simple model to store the opportunities with a description,
a start and end date, a link to a party and an optional comment.
Our model in opportunity.py
file currently looks like this:
from trytond.model import ModelSQL, fields
class Opportunity(ModelSQL):
"Opportunity"
__name__ = 'training.opportunity'
_rec_name = 'description'
description = fields.Char("Description", required=True)
start_date = fields.Date("Start Date", required=True)
end_date = fields.Date("End Date")
party = fields.Many2One('party.party', "Party", required=True)
comment = fields.Text("Comment")
As you can see a Model must have a __name__
attribute.
This name is used to make reference to this object.
It is also used to build the name of the SQL table to store the opportunity
records in the database.
The _rec_name
attribute defines the field that
will be used to compute the name of the record.
The name of the record is its textual representation.
The party
field is a relation field (Many2One) to another Model of Tryton
named party.party
.
This model is defined by the party
module.
Register the model in the Pool#
Once a Tryton model is defined, you need to register it in the
Pool
.
This is done in the __init__.py
file of your module with the following
code:
from trytond.pool import Pool
from . import opportunity
def register():
Pool.register(
opportunity.Opportunity,
module='opportunity', type_='model')
Models in the pool are inspected by Tryton when activating or updating a module in order to create or update the schema of the table in the database.
Activate the opportunity module#
Now that we have a basic module, we will use it to create the related table into the database created.
First we must edit the tryton.cfg
file to specify that this module
depends on the party
and ir
module.
We need to do this because the Opportunity
model contains the party
field which refers to the Party
model.
And we always need the ir
module which is always included in Tryton server.
Here is the content of our tryton.cfg
file:
[tryton]
version=x.y.0
depends:
ir
party
As we defined a new dependency, we must refresh the installation with:
$ python -m pip install --editable opportunity
Now we can activate the opportunity
module and its dependencies:
$ trytond-admin -d test -u opportunity --activate-dependencies
This step has created the tables into your database. You can check it with the sqlite3 command line:
$ sqlite3 ~/db/test.sqlite '.schema training_opportunity'
CREATE TABLE "training_opportunity" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
"comment" TEXT,
"create_uid" INTEGER,
"create_date" TIMESTAMP,
"description" VARCHAR,
"end_date" DATE,
"start_date" DATE,
"write_date" TIMESTAMP,
"party" INTEGER,
"write_uid" INTEGER);
The next step will be displaying record.