We will start by creating a simple OpenERP module with only one form and one table in PostgreSQL database with no relations at all. Later on we will extend this simple module in more complex module with multiple forms, multiple tables and relations step by step. I assume that u have installed OpenERP All-In-One that includes OpenERP Server, OpenERP Web Client, OpenERP GTK Client and PostgreSQL and I assume that its configured and working fine.
I am using Ubuntu 9.10
- Open terminal and write sudo nautilus. Than enter your password. A file browser window will open. Browse to the addons folder in the openerp-server. The path would be something like this e.g, /usr/local/lib/python2.6/dist-packages/openerp-server/addons/
- create a new folder in it and name it “sim”, short for Student Information Management. Open the folder.
- Create the following files in the sim folder.
- __init__.py (its “double underscore init double underscore.py”). Python file.
- __terp__py (its “double underscore terp double underscore.py”). Python file.
- sim.py Python file
- sim_view.xml XML file.
- Open __init__.py file and write the following code.
import sim
sim is the name of our module that we are developing and its the name our major python file that will include all our python code. Save the file and exit
- Open __terp__.py file and write the following code.
{ 'name': 'Student Information Management', 'version': '0.1', 'category': 'Tools', 'description': """This module is for the Student Information Management.""", 'author': 'Mir Nauman Tahir', 'website': 'https://mirnauman.wordpress.com/', 'depends': ['base'], 'init_xml': [], 'update_xml': ['sim_view.xml'], 'demo_xml': [], 'installable': True, }
You can write your own strings in “name, description,author and website”Save the file and exit
- Now open sim.py file and write the following code.
class student(osv.osv): _name = "sim.student" _description = "This table is for keeping personal data of student" _columns = { 'name': fields.char('Registration Number',size=256,required=True), 'student_name': fields.char('Student Name',size=256,required=True), 'father_name': fields.char('Father Name',size=256), 'gender':fields.selection([('male','Male'),('female','Female')],'Gender'), 'contact_no':fields.char('Contact Number',size=256) } student()
This code will create a table in the database “student”. The value for _description can be any string of your choice. _column is a dictionary and contains all the column names of the table with their data types. In side the parenthesis is the caption for that field that will appear on the form, its size and “required=True” shows that its a must to enter this field. The “field.selection” creates a drop down list control that has two items, Male and Female. The “student()” shows the end of the class. Some precautions. Python is a language that works with indentation. One space extra or less can generate billions of errors and you wont get a proper clue, if you are not using the proper editor.
- Open the sim_view.xml file and write the following code
<?xml version="1.0"?> <openerp> <data> <!-- ============== student================= --> <!-- 1st part of the sim_view start--> <record model="ir.ui.view" id="student_form"> <field name="name">Student</field> <field name="model">sim.student</field> <field name="type">form</field> <field name="arch" type="xml"> <form string="Student"> <field name="name"/> <field name="student_name"/> <field name="father_name"/> <field name="gender"/> <field name="contact_no"/> </form> </field> </record> <!-- 1st part of the sim_view end--> <!--2nd part of the sim_view start--> <record model="ir.ui.view" id="student_tree"> <field name="name">Student</field> <field name="model">sim.student</field> <field name="type">tree</field> <field name="arch" type="xml"> <tree string="Student"> <field name="name"/> <field name="student_name"/> <field name="father_name"/> <field name="gender"/> <field name="contact_no"/> </tree> </field> </record> <!--2nd part of the sim_view end--> <!-- 3rd part of the sim_view start--> <record model="ir.actions.act_window" id="action_student"> <field name="name">Student</field> <field name="res_model">sim.student</field> <field name="view_type">form</field> <field name="view_mode">tree,form</field> </record> <!--3rd part of the sim_view end--> <!--4th part of the sim_view start--> <menuitem name="SIM/Student/StudentInfo" id="menu_sim_student" action="action_student"/> <!--4th part of the sim_view end--> </data> </openerp>
From my perspective every view file for an OpenERP module will consist of atleast 3 or 4 parts
- The form part.
- The tree part.
- The action part.
- The menu part.
And thats how I have divided the above documents in 4 parts. Its not necessary that an OpenERP module must have all the above 4 parts. It can have form,action and menu parts or tree,action and menu or all the four parts. The 1st part will create a form view. It should be kept in mind that the <field name=”model”>sim.student</field>, the sim.student should be exactly the same as the value for the _name=”sim.student”. This refers to the sim_student table for which we are creating this form. The <field name=”type”>form</field> shows which view type it is, a tree or a form. The next important block is the <field name=”name”/>,<field name=”student_name”/>….. the values for the name here will be the exact column names from our sim.py file. The rest of the things don’t need much explanation for now. The 2nd part contain exactly the same things as the form view with very few differences like the type is tree here instead of form. And the column fields are encapsulated in the . This will create a grid type view displaying all the rows and column that are entered. This grid type view is called tree view. The 3rd part is the action. In res_model give the exact model name or the table name from our sim.py file, I.e “sim.student”. Specify a view_type that is form and view mode that is tree, form. The action defines when a menu is clicked for this action which form will it open. In the menu, id of this action will be used. The 4th part is related to creating the menu. A menu has the following important parts that are name,id,action and in some cases parent. There are two ways for creating a menu. I have used a way that I found more simple. “SIM/Student/StudentInfo”, SIM is the primary menu and appears in the left pane. When SIM is clicked Student menu will appear in the right pane. When student is clicked StudentInfo will appear and when StudentInfo is clicked it will open the form that is defined in the action for this menu. Save the file, exit. Now run OpenERP client. Install the current developed module and check it in working condition. More complexities to this tutorial will be added soon.
23 comments
Comments feed for this article
September 3, 2010 at 8:02 am
Waqar Afridi
I have tried the above. with one form it worked fine. when i created another form in module, i received the following error.
Error occurred while validating the field(s) res_model,src_model: Invalid model name in the action definition.
my code seems to fine. i have checked it many time. but still am getting this error.
need help
September 6, 2010 at 5:37 am
mirnauman
When ever u make a change to the code, and u think ur code is perfect and fine in every aspect, u have to restart the server.
open the terminal and type
sudo /etc/init.d/openerp-server restart
this will restart the server. than try running ur module again. hope it will work.
September 6, 2010 at 5:39 am
mirnauman
Also remove the Document Start and Document End comments from the xml file. The xml file will not work with these comments. its just for the sake of understanding that from where the xml document starts and where it ends.
September 7, 2010 at 5:04 am
Waqar Afridi
Thanx Mir, It Worked, I will be Posting some New and Challenging Errors very soon, Hope you will be Ready 🙂
February 23, 2011 at 7:06 pm
Aamir Riaz
Dear Nauman,
I saw your post “OpenERP Module Development Part-1” on your blog. That was a very nice & simple tutorial and also was very helpful for me. Now I am trying to make my own very simple module but I couldn’t succeeded yet. Following is the error text:
File “/home/apus/Downloads/openerp-server-6.0.1/bin/netsvc.py”, line 489, in dispatch
result = ExportService.getService(service_name).dispatch(method, auth, params)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/service/web_services.py”, line 599, in dispatch
res = fn(db, uid, *params)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/osv.py”, line 122, in wrapper
return f(self, dbname, *args, **kwargs)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/osv.py”, line 176, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/osv.py”, line 167, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/orm.py”, line 1787, in name_search
return self._name_search(cr, user, name, args, operator, context, limit)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/orm.py”, line 1801, in _name_search
res = self.name_get(cr, access_rights_uid, ids, context)
File “/home/apus/Downloads/openerp-server-6.0.1/bin/osv/orm.py”, line 1767, in name_get
[self._rec_name], context, load=’_classic_write’)]
KeyError: ‘name’
The contents of the source files are:
module folder name
myx
__init__.py
import myx
__openerp__.py
{
‘name’: ‘myx’,
‘version’: ‘1.0’,
‘category’: ‘myx’,
‘description’: “””
Testing
“””,
‘author’: ‘Aamir Riaz’,
‘website’: ‘http://www.myx.org’,
‘depends’: [],
‘init_xml’: [],
‘update_xml’: [‘myx_view.xml’],
‘demo_xml’: [],
‘test’: [],
‘installable’: True,
‘active’: False,
}
myx.py
from osv import fields, osv
class myx_dptx(osv.osv):
“”” myx dptx “””
_name = ‘myx.dptx’
_description = ‘dptx’
_columns = {
‘dcode’: fields.char(‘Code’, size=5, required=True, help=”Code of the Dpt”),
‘dname’: fields.char(‘Name’, size=50, required=True, help=”Name of the Dpt”),
‘dactive’:fields.boolean(‘Active’, help=”If True then Dpt Status would be Active”),
}
_defaults = {
‘dactive’: lambda *a: 1
}
myx_dptx()
class myx_empx(osv.osv):
“”” myx empx “””
_name = ‘myx.empx’
_description = ’empx’
_columns = {
‘ecode’: fields.char(‘Code’, size=5, required=True, help=”Code of the Emp”),
‘ename’: fields.char(‘Name’, size=50, required=True, help=”Name of the Emp”),
‘dptx_id’: fields.many2one(‘myx.dptx’, ‘Department’),
‘eactive’:fields.boolean(‘Active’, help=”If True then Emp Status would be Active”),
}
_defaults = {
‘eactive’: lambda *a: 1
}
myx_empx()
myx_view.xml
dptx
myx.dptx
form
dptx
myx.dptx
tree
dptx
myx.dptx
empx
myx.empx
form
empx
myx.empx
tree
empx
myx.empx
If you have some spare time then please review my code and tell me what is wrong with them or if don’t then please refer me anything which can be helpful to me.
Thanks & Regards,
February 23, 2011 at 7:08 pm
Aamir Riaz
As you said earlier in your post:
“We will start by creating a simple OpenERP module with only one form and one table in PostgreSQL database with no relations at all. Later on we will extend this simple module in more complex module with multiple forms, multiple tables and relations step by step.”
And that post was published on September 2, 2010 almost 5 months ago, I am waiting for your next tutorial related with your previous post.
February 23, 2011 at 7:13 pm
Aamir Riaz
Finally after 1 month my problem has been resolved, I got a clue that “name” field is required with many2one relation.
March 1, 2011 at 11:21 am
Aamir Riaz
Waiting for your next post with “one2many” relationship.
June 13, 2011 at 9:29 am
Vincent
I’m a newbie openerp developer.
I have successfully installed and run this module. But I could not find these files in my openerp directories to make some changes. After install where did it goes ?
June 14, 2011 at 5:28 am
mirnauman
Check the addons folder, the exact location might look somewat like
/usr/local/lib/python2.6/dist-packages/openerp-server/addons/
December 16, 2011 at 1:16 pm
aaqil ali
good explanation for begginers
April 4, 2012 at 12:56 am
amrimansor
your file sim.py missing this code to run on 6.1. pls add this line at the very first line..
from osv import fields, osv
July 23, 2012 at 10:14 am
nida
Thanks alot Mir. Its working..:)…
August 9, 2012 at 12:10 pm
Haley
Im new to openerp… im not at ease with linux. does the following work on windows 7 with openerpallinone 6.0 installed?. thxs to help
August 15, 2012 at 8:44 pm
Mir
yes it will work.
August 9, 2012 at 5:40 pm
Haley
Hello it works!. thanks :). thanks also if you could help for the more complex module with multiple forms, multiple tables and relations ….
August 15, 2012 at 8:48 pm
Mir
i guess u can find alot of good articles on openerp at http://mohsinpage.wordpress.com/.
i completed the projects that i started in openerp but never got enough time to document them all, so this series was never completed and for now i have moved on to some other things that am currently working with. hope u wont mind.
August 27, 2012 at 1:45 am
shineupmylife88
im new in openerp and i have to create new module. may i know when these module already have been install, where the data have been store. i meant, do i have to create database or it created it self.?seriously i dont know nothing about openerp but i have to do it since im in internship condition and i have to self learning about this,..
August 27, 2012 at 6:35 am
Mir
when u create the sim.py file. the db will be created accordingly, automatically.
January 4, 2013 at 3:11 am
likeat
i’ve followed your instruction, then i restart my openerp service, i use openerp 6 all in one, then when i check in module, there is no one module ‘sim’, what should i do?
March 11, 2013 at 11:28 am
Priyan
please tell me when we going to create another module with the help of your code, what sections are need to change on that files.?
then need to clarify when we create & add a folder to addon folder, what is the corrct provedure to install it
need to restart server.?logout and again login.?refresh the page.?
need a reply from yours……….friends……..
thanks…….ur module successfully run in my PC with OpenERP 7 & with Ubuntu 12.10
March 11, 2013 at 11:47 am
Mir
Dear Priyan,
i have given up on open erp coz i couldn’t manage time for providing support for so many technologies. Open ERP has evolved a lot since i have written this article. but still u can try the following,
Create a new folder and name it with wat u want ur new module to be. Also create ur python and xml files with the same name as that of ur folder. wrte “urfolder_view.xml” in the terp file. In your python file, write urfoldername.urclassname. the rest depends on the logic of ur module.
Go to modules and simply check for updates. refresh the list. it will have ur new module. install it.
Note:- this can be alot different in OpenERP as i have no idea if they have changed the interface and the procedure in the current versions.
August 29, 2013 at 1:10 pm
openerp | Annotary
[…] mirnauman.wordpress.com […]