• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2174
  • Last Modified:

OpenERP Functional Field

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?
0
infinitegatorade
Asked:
infinitegatorade
  • 3
  • 2
1 Solution
 
Sinoj SebastianCommented:
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

0
 
infinitegatoradeAuthor Commented:
That looks like Python.  Which file would that go in?  I only see XML files when editing the view.
0
 
Sinoj SebastianCommented:
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.
0
 
infinitegatoradeAuthor Commented:
Thanks!
0
 
infinitegatoradeAuthor Commented:
Note: the python needs to be added to /opt/openerp/server/openerp/addons/SOMETHING/SOMETHING.py
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now