adding additional fields using a custom module in openerp 7

Upload: herve-egnakou

Post on 01-Mar-2016

30 views

Category:

Documents


0 download

DESCRIPTION

OpenERP

TRANSCRIPT

  • My eyes, the goggles do nothing!

    Getting started on OpenERP can be a bit daunting, initially this behemoth can be really painful.However once you get familiar with the system everything is really easy. In this tutorial I'll go throughthe steps involved in adding an additional field to an existing model and adding it to an existing view.We'll also do this by creating a custom module, so we will also cover some of the basics for doing thatas well.

    Some context you say?

    For this example, assume that we are about to migrate out internal project management system toOpenERP, and when we migrate any existing projects over we want an extra field where we can put areference to the original project management system ID. Essentially modifying the OpenERP projectmodel.

    Getting started

    We'll start with a clean install of OpenERP version 7, I wont go into how to install, however I might doso at a later date if there is enough demand. For now you can check out this helpful installation tutorialhere (http://www.theopensourcerer.com/2012/12/how-to-install-openerp-7-0-on-ubuntu-12-04-lts/) . Once the database isset up go to Settings >> Modules >> Apps and enable the Project management app.

    So now that's done, if you go into the Projects view and click on create you should get something likeso.

    Submitted by thepearson on Thu, 08/22/2013 - 21:04

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    1 sur 12 11/11/2014 13:55

  • What we want from this view is to add a field under where the customer field is, called Legacy ID rightabout where I've drawn the red block in this image.

    So lets get to it.

    Where to put things

    The module directory in OpenERP is located in the folder /OPENERP_ROOT/openerp/addons youcan put your modules in there or symlink them into that directory but to make upgrading OpenERPcore cleaner, there's a better way.

    Create a new folder outside of the OPENERP_ROOT folder and call it something useful, I've calledmine custom_modules

    In the newly created module directory create an __init__.py file with the following code.

    from openerp.modules import get_module_resource, get_module_path

    This tells the system that this directory will contain addons/modules. Now when we run OpenERP, we

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    2 sur 12 11/11/2014 13:55

  • have to tell the system about this new additional addons directory. This can be done in the config file(good for production servers) with the addons_path or via the command line (better for development)at start up like so.

    python openerp-server --addons-path=openerp/addons,../custom_modules

    However, if you run this right now, it will fail with the following error

    openerp-server: error: option --addons-path: The addons-path '../custom_modules' does not seem to a be a valid Addons Directory!

    This is because we haven't actually created any modules there yet, so lets get on with it.

    A basic module

    Create a new folder in the custom_modules folder and name it legacy_projects. In this folder createan __init__.py file but leave it empty for now. For OpenERP to recognise it We also need to create amodule declaration file. In the same folder create a file called __openerp__.py inside this file we willput the following.

    { 'name': "Legacy project integration", 'version': "1.0", 'author': "thepearson", 'category': "Tools", 'depends': ['project'], 'data': [], 'demo': [], 'installable': True,}

    I wont go into details about this file for now, (you can read up on it here (https://doc.openerp.com/6.0/developer/2_4_module_development/module_structure/#openerp-module-descriptor-file-openerp-py) ) but suffice tosay that it tells OpenERP about our module. Now, if you fire up the server it should kick into action.

    Lets do that.

    At this point, to help with enabling custom modules it's useful to enable some permissions on our useraccount. Log in (with administrator or an account that has administrative privs) and navigate toSettings >> Users and edit your user account. Enable the Technical features check-box underusability.

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    3 sur 12 11/11/2014 13:55

  • Sweet, save your account and refresh your browser. You should now see a whole bunch of extramenu items on the left. One we are looking for in particular is Update modules list click on that andthen click update. This will refresh the system module cache.

    Now if we go to the Installed modules section and delete the tag Installed from the search box andsearch for legacy project the module you just created should display.

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    4 sur 12 11/11/2014 13:55

  • Great, this is how we'd also install the module, but before we do lets make it do something useful first.

    Uh, a little help?

    Something that makes development in OpenERP a little easier is Developer Mode so lets turn that onnow. Log in as administrator, click on the dropdown menu at the top right and then click on AboutOpenERP in the modal that pops up is an Activate the developer mode link, click on that.

    Developer mode gives you the ability to inspect views and models from the interface so you don'thave to go trawling through code and/or database schemas to figure out what's going on. It gives youa menu at the top of the page with some handy developer links and if you're looking at a view youwant to modify you can hover over a field to see technical information about it, like below.

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    5 sur 12 11/11/2014 13:55

  • As you can see from that screenshot our model (or object) is called project.project We'll use that inthe next step.

    Making changes to an existing model

    First up we want to tell the OpenERP about the new field, OpenObject (https://launchpad.net/openobject) isthe ORM framework behind OpenERP and adding additional fields to existing models is easy.

    First up lets create a file inside our legacy_projects module and save it it legacy_projects.py. Wealso need to tell our module to load that file, so in the __init__.py file put the following:

    import legacy_projects

    Sweet, now in the legacy_projects.py file put the following.

    from openerp.osv import fields, osv

    class legacy_projects(osv.osv):

    _inherit = "project.project"

    _columns = { 'legacy_projects_id': fields.integer('Legacy project ID', size=11) }

    _defaults ={ 'legacy_projects_id': 0 }

    legacy_projects()

    In the first line we import the required classes, then we define our class.

    The real magic starts with the line _inherit = "project.project basically we are telling the system thatour object inherits an existing object.

    _columns is where we define our field, legacy_projects_id. I have created it as an integer, gave it atitle and set the max ength to 11.

    There is a bunch of different fields you can add to an object, again I wont go into then but you can find

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    6 sur 12 11/11/2014 13:55

  • all of them here (https://doc.openerp.com/trunk/server/03_module_dev_02/#type-of-fields) .

    Lastly we define some _defaults for fields. In this instance (while not really helpful, but I put it in forclarity) we default the field legacy_projects_id to 0.

    That's the model/object modifications done now. Time to add the interface.

    The user interface

    In OpenERP the UI is controlled and formatted using XML files.

    In our module folder create a new file called legacy_projects.xml. We also need to let OpenERPknow about it, so update the line in the __openerp__.py file that reads:

    'data': [],

    and make it read:

    'data': ['legacy_projects.xml'],

    Now in the legacy_projects.xml file place the following:

    project.project.form project.project

    Without getting into too much detail about the views the important pieces of this code are the recordtag where we define our view's id.

    Next we have the view name, object (model) and a reference to the view we are inheriting.

    Since, we are inheriting another modules view, we dont get to define this info, we have to look it up. Todo this information, make sure developer mode is enabled, go to the view we are changing then in thedeveloper drop down menu at the top click on Manage views You'll see something like the following.

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    7 sur 12 11/11/2014 13:55

  • In the table you can see View name, Object and External ID these map to name, model andinherit_id respectfully.

    The next section has an Xpath expression in it with the position attribute, this tells the system to putthe field legacy_projects_id AFTER the node represented in the Xpath.

    If you're wondering how you figure out where you want to put the field. Use Development mode.While in the Manage views section, with the current view selected, click edit. That's your view XML.Cool huh?

    Cool, now make it work.

    Ok, ok, enough with the details, let's go turn this thing on. Navigate to Settings >> InstalledModules. Click on the Update module list link on the left, and then like before search for you'remodule. This time lets enable it.

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    8 sur 12 11/11/2014 13:55

  • NOTE: If the above doesn't work, you may need to restart the server and then either update oruninstall/install your module

    Things should whir away for a while and if all is well, your module will be enabled. Let's go an create anew project and see, navigate to the Projects page and hit Create

    Bingo Look now we have a new field under the Customer field Legacy Project ID and hovering overit in developer mode, we can see it's the field we created.

    If we create a project and enter a value in this field it will be saved to the database, OpenERP does allthe work for us. In fact this post seems awfully long for how little code we've actually done. Here iswhat our saved Project looks like.

    Are you done yet?

    So there you have it, doesn't OpenERP look easy. :D Hopefully this totorial has made sense and has

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    9 sur 12 11/11/2014 13:55

  • been helpful. I've posted the source for the code in this tutorial on github, there's a link after the article.Let me know in the comments if you have any questions, or if you want me to cover anotherOpenERP topic.

    Source

    Source for this module on github (https://github.com/thepearson/legacy_projects)

    Tags

    openerp (/tag/openerp) python (/tag/python) module (/tag/module)Category

    Tutorial (/category/tutorial)

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    10 sur 12 11/11/2014 13:55

  • 34 Comments

    gonssal Just thank you.

    Razor Thanks for this great tutorial, this is to the point and very very useful.

    Could you please do more tutorials on openerp modules, specially on how to removecertain things using modules like,How to remove "About OpenERP" and "My OpenERP.com account" from thedropdown menu at the top right.How to remove "manage databases" from login screen.Things like that

    Thanks again.

    Craig Pearson Mod

    Thanks for the feedback. I am currently not doing a lot of OpenERP work, butwill keep some of these ideas in mind for when I get some spare time andback into the OpenERP headspace.

    Francisco Luz Excellent tutorial. It helped me allot at grasping the logic behind OpenERP's moduledevelopment.

    Jose G That's was beautiful! snif! snif! Thank you!

    Craig Pearson Mod

    Thanks. I was going for beautiful. :-)

    Javier Thank you!!

    Craig Pearson Mod

    You're welcome

    Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    11 sur 12 11/11/2014 13:55

  • Adding additional fields using a custom module in OpenERP 7. | Pixeli... http://www.pixelite.co.nz/article/adding-additional-fields-using-custo...

    12 sur 12 11/11/2014 13:55