Extend model

Sometimes we want to extend an existing Model to add Field or methods. This can be done using the extension mechanism of Tryton which can combine classes with the same __name__ that are registered in the Pool.

Extend the Party model

Let’s add an opportunities field on the party.party model. The model in party.py file of our module looks like this:

from trytond.model import fields
from trytond.pool import PoolMeta

class Party(metaclass=PoolMeta):
    __name__ = 'party.party'
    opportunities = fields.One2Many(
        'training.opportunity', 'party', "Opportunities")

This new class must be register in the Pool. So in __init__.py we add:

from . import party

def register():
    Pool.register(
        ...,
        party.Party,
        module='opportunity', type_='model')

Extend the Party view

Now that we added a new field to the party.party Model, we can also add it the form view. This is done by adding a ir.ui.view record that inherit the party form view of the party module. Here is the content of the party.xml file:

<tryton>
   <data>
      <record model="ir.ui.view" id="party_view_form">
         <field name="model">party.party</field>
         <field name="inherit" ref="party.party_view_form"/>
         <field name="name">party_form</field>
      </record>
   </data>
</tryton>

The type is replaced by:

inherit

A reference to the XML id of the view extended prefixed by the name of the module where the view is declared.

The content of the inheriting view must contain an XPath expression to define the position from which to include the partial view XML. Here is the content of the form view in view/party_form.xml:

<data>
   <xpath expr="/form/notebook/page[@name='identifiers']" position="after">
      <page name="opportunities" col="1">
         <field name="opportunities"/>
      </page>
   </xpath>
</data>

And finally we must declare the new XML data in the tryton.cfg file:

[tryton]
...
xml:
   ...
   party.xml

Update database

As we have defined new field and XML record, 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 field on the party:

$ trytond

Let’s use a wizard to convert the opportunity.