We help IT Professionals succeed at work.

OpenERP Functional Field

Medium Priority
2,328 Views
Last Modified: 2012-08-10
How do I create a functional field in OpenERP?  For example, given an existing "width" and "height" fields, how would I create an "area" field that automatically calculates itself using the other two fields?
Comment
Watch Question

Sinoj SebastianCTO & OpenERP Project manager

Commented:
A function field is just like other fields with a function to calculate the value.

It may look like this

'area': fields.function(_calculate_area, string='Area', type="float", store=True),

Open in new window


In this case the _calculate_area is a function pointer and you should have that function in the same class.

Here is a sample function (Please note that I assume you have width and height defined as float or integer field on the same model)

    def _calculate_are(self, cr, uid, ids, name, args, context=None):
        res = {}
        for object in self.browse(cr, uid, ids):
            area = object.width * object.height
            res[object.id] = area
        return res

Open in new window

Author

Commented:
That looks like Python.  Which file would that go in?  I only see XML files when editing the view.
CTO & OpenERP Project manager
Commented:
Function field should be defined in python file and then can be included in XML view. This code should go in to the same py file were you have the height and width fields defined.

Author

Commented:
Thanks!

Author

Commented:
Note: the python needs to be added to /opt/openerp/server/openerp/addons/SOMETHING/SOMETHING.py

Explore More ContentExplore courses, solutions, and other research materials related to this topic.