My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
HelloWorld  
Creating a new Module
Featured
fr , en
Updated Feb 27, 2012 by cedkrier@gmail.com

Module Creation

We will start by creating a module called HelloWorld, in the beginning our module will only have an entry in the main menu. We will add more features later on. Now let's get started.

Step 1: Creating the HelloWorld module

Create the directory called helloWorld in the trytond/modules directory.

In Linux you create the directory like this:

cd  /usr/local/tryton/trytond/trytond/modules/
mkdir helloWorld

Inside this directory put the file __init__.py, this file will call our .py files, as in the following line:

from hello import *

Then create the file __tryton__.py with the following lines:

{
    'name' : 'Hello World',
    'version' : '0.0.1',
    'author' : 'nickname',
    'email': 'correo@example.com',
    'website': 'http://www.tryton.org/',
    'description': 'Hello World with menus',
    'depends' : [
        'ir',
    ],
    'xml' : [
        'hello.xml',
    ],
    'translation': [
    ],
}

Add the file hello.py

from trytond.model import ModelView, ModelSQL, fields
class Hello(ModelSQL, ModelView):
    'HelloWorld'
    _name = 'hello.hello'
    _description = __doc__

Hello()

And then in the last file hello.xml we put:

<?xml version="1.0"?>

<tryton>
    <data>
        <menuitem name="Hello World" id="menu_hello_world" sequence="10" icon="tryton-users"/>
    </data>
</tryton>

With this we have the first module to import, which will show in the main menu without any information.

Note If you have problem with the icon, remove it from the hello.xml file.

Step 2: Extending our model and adding views

Now that our module is working we will add some more features. Let's extend our model and add two views to the module. To extend our model we edit the file hello.py and add these two fields to the class Hello:

    name = fields.Char('Name')
    greeting = fields.Char('Greeting')

with this our file will be:

from trytond.model import ModelView, ModelSQL, fields
class Hello(ModelSQL, ModelView):
    'HelloWorld'
    _name = 'hello.hello'
    _description = __doc__
    name = fields.Char('Name')
    greeting = fields.Char('Greeting')

Hello()

This tells tryton to create two fields, both of type Varchar or String.

After this we edit the file hello.xml and add the following groups of lines. Each group of lines is preceded with an explaination. For more information read the Views section of the Tryton documentation found at tryton.org.

First we create a view of type tree which will list our model hello.hello with its two fields: name and greeting.

        <!-- Vista para el Arbol del saludo-->
        <record model="ir.ui.view" id="hello_view_tree">
            <field name="model">hello.hello</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <![CDATA[
                <tree string="Hello World">
                    <field name="name"/>
                    <field name="greeting"/>
                </tree>
                ]]>
            </field>
        </record>

Secondly we create a view of type form which will show a single record of our model hello.hello in a form with with the two labels and two fields: name and greeting.

       <record model="ir.ui.view" id="hello_view_form">
            <field name="model">hello.hello</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
              <![CDATA[
              <form string="Hello">
                <label name="name"/>
                <field name="name"/>
                <label name="greeting"/>
                <field name="greeting"/> 
              </form>
              ]]>
            </field>
       </record>

Create an event to open a new tab(window). This event will be used to connect the menu Hello World with the tree and form views. Note the id of this event as it will be used later.

        <!-- View for the main menu and the event -->
        <record model="ir.action.act_window" id="act_hello_world_form">
            <field name="name">Hello World</field>
            <field name="res_model">hello.hello</field>
        </record>

With the ids of the event and the two views defined before we add two fragments to attach the id to the window and the view used to populate the window. The field view indicates which view will be used and the field act_window indicates the event that will activate the view.

        <!-- View that connect the form in the tree with the specification -->
        <record model="ir.action.act_window.view" id="act_hello_form_view1">
            <field name="sequence" eval="10"/>
            <field name="view" ref="hello_view_tree"/>
            <field name="act_window" ref="act_hello_world_form"/>
        </record>

        <!-- View for the edition or the Form of hello-->
        <record model="ir.action.act_window.view" id="act_hello_form_view2">
            <field name="sequence" eval="20"/>
            <field name="view" ref="hello_view_form"/>
            <field name="act_window" ref="act_hello_world_form"/>
        </record>

We use ids for each record throughout the xml file. This allows other modules to identify and reuse or extend parts of our module.

Now finally at the end of our xml file we add one more fragment to join the views with the last menu:

        <menuitem parent="menu_hello_world" sequence="1"
            id="menu_hello_world_form" icon="tryton-list" action="act_hello_world_form"/>

Step 3: Creating a report

Add the following lines to the file 'hello.xml' into the /data tag:

        <record model="ir.action.report" id="report_hello">
            <field name="name">Hello</field>
            <field name="model">hello.hello</field>
            <field name="report_name">hello.helloworld</field>
            <field name="report">helloWorld/hello.odt</field>
        </record>
        <record model="ir.action.keyword" id="report_hello_world">
            <field name="keyword">form_print</field>
            <field name="model">hello.hello,0</field>
            <field name="action" ref="report_hello"/>
        </record>

Now create the file hello.odt inside the directory .../trytond/trytond/modules/helloWorld.

In this file add the lines:

<for each="hello in objects">
<hello.name>
</for>

Each line, must be added using OpenOffice writer, in the menu: Insert->Fields->Others..., in the tag 'Functions', with the type 'Placeholder' and the format 'Text', and fill the field Placeholder with each line, vg: 'for each="hello in objects"'.

Other way to do this is copying the file '.../trytond/trytond/modules/party/label.odt', and editing it by pressing the right button in the mouse, and copying and pasting to add new lines.

Comment by project member ggd...@gmail.com, Sep 4, 2009

Hi!

Thanks a lot for making this page much more clear than before.

thumb up for extending/improving the docs.

Sincerely, Gour

Comment by gmartind...@gmail.com, Aug 11, 2010

Once tested the module... when I try to uninstall, ther is no option. How can I uninstall it?

Comment by project member cedkrier@gmail.com, Aug 11, 2010

There is not yet uninstall option see http://bugs.tryton.org/roundup/issue322

Comment by chris.ly...@gmail.com, Jul 12, 2011

Version 2.0 view_type is not recognized in ir.action.act_window here the correction

<!-- View for the main menu and the event --> 
<record model="ir.action.act_window" id="act_hello_world_form"> 
  <field name="name">Hello World</field> 
  <field name="res_model">hello.hello</field> 
</record>
I just remove the line : <field name="view_type">form</field>
Comment by dauren.n...@gmail.com, Jul 21, 2011

1) what about tryton 2.0? how can I create my custom module in tryton 2.0? 2) in ubuntu /usr/local/tryton/trytond/trytond/modules/ is not exist

Comment by rros...@gmail.com, Aug 23, 2011

<dauren>: in debian squeeze the directory is /usr/local/lib/python2.6/dist-packages/trytond-2.0.1-py2.6.egg/trytond/modules/ by default installation of source code with 'python setup.py install'. just look in your filesystem with search file.

Comment by yogeshd....@gmail.com, Nov 9, 2011
debian /usr/local/tryton/trytond/trytond/modules/ is not exist

in my pc path is /usr/share/pyshared/trytond

plz give me reply how can create new project.........

Comment by mathi...@m9s.biz, Nov 10, 2011

> Comment by yogeshd....@gmail.com, > debian /usr/local/tryton/trytond/trytond/modules/ is not exist

This path would be valid for a dev installation like in http://code.google.com/p/tryton/wiki/InstallationOnDebian#Option_2:_Running_from_sources

> in my pc path is /usr/share/pyshared/trytond > plz give me reply how can create new project.......

This path is valid, if you installed Debian packages like in http://code.google.com/p/tryton/wiki/InstallationOnDebian#Option_1:_Installation_from_package_management

So in your case put your module under /usr/share/pyshared/trytond/modules

Comment by se...@koolpi.com, Dec 3, 2011

With the lastest trunk, got an error Invalid XML error on:

<tree string="Hello World">
<field name="name" select="1"/> <field name="greeting" select="1"/>
</tree>

If i remove the select attribute for both fields it works.

Comment by project member udo.spallek@gmail.com, Dec 5, 2011

@sergi: Thanks for feedback. I fix the wiki text. May someone please fix http://wiki.opdevel.com/Tryton/CreacionModulo?action=AttachFile&do=view&target=helloWorld.tar.gz ?

Comment by yogeshd....@gmail.com, Dec 9, 2011

reply by mathi....@m9s.biz

thank u for giving reply i following your given confirmation path and get result........

thank u very much

Comment by criscode...@gmail.com, Jan 27, 2012

Hello

I have download tryton-server_1.6.1-2_all and tryton-client_1.6.1-2_all. And server and client sucessfully but when added the module helloworld, it not show me in the modules list. This is the path where i have added the module Software/tryton-server_1.6.1-2_all/usr/share/pyshared/trytond/modules.

Would you please help?

Thanks Criscoder

Comment by project member udo.spallek@gmail.com, Jan 28, 2012

Hello criscoder.ozas,

it seems to me you install Tryton with Debian package management. For programming Tryton I suggest to install Tryton from sources1?. Additionally Tryton version 1.6 is quite old2?. The current release is 2.2.

To answer your question: There path you mentioned is usually sym-linked from another destination in Debian, e.g. /usr/lib/python2.6/dist-packages/trytond/modules/

1? http://code.google.com/p/tryton/wiki/InstallationMercurial 2? http://www.tryton.org/downloads.html

Comment by tristian...@gmail.com, May 23 (6 days ago)

Do all my module classes have to be in a single file?

Comment by project member albert.n...@gmail.com, May 23 (6 days ago)

@tristian.azuara

No, they can be in several files but you must import those files to init.py, just like in any other python module.

Comment by tristian...@gmail.com, May 24 (5 days ago)

Also, can I split my xml into multiple files?


Sign in to add a comment
Powered by Google Project Hosting