Reputation: 145
I am finding the documentation for openerp 7 to be frustrating.
I am attempting to call a function from an on_change event to perform a calculation and place the result into another field within the same row.
from openerp.osv import osv, fields
class degree_day(osv.osv):
_name = "degree.day"
_columns={
'date': fields.date('Date'),
'high_temp': fields.integer('High Temp'),
'low_temp': fields.integer('Low Temp'),
'heat_degree_day': fields.integer('Heat Degree Day' ),
'hw_degree_day': fields.integer('HW Degree Day' ),
}
def generate_degree_day(self, cr, uid, ids, high_temp = 0, low_temp = 0, context=None):
""" On change of temperature generate degree day numbers
@param high_temp: The day's high temperature
@param low_temp: The day's low temperature
"""
if not (high_temp and low_temp):
return
temp = 65 - (high_temp + low_temp) / 2
if temp < 0:
temp = 0
heat_degree_day = temp
hw_degree_day = temp + 5
<>
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="degree_day_tree" model="ir.ui.view">
<field name="name">degree.day.tree</field>
<field name="model">degree.day</field>
<field name="arch" type="xml">
<tree string="Degree Day List" editable="bottom">
<field name="date" />
<field name="high_temp" on_change="generate_degree_day(high_temp, low_temp)" />
<field name="low_temp" />
<field name="heat_degree_day" />
<field name="hw_degree_day" />
</tree>
</field>
</record>
<record id="show_degree_day" model="ir.actions.act_window">
<field name="name">Degree Day</field>
<field name="res_model">degree.day</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Degree Day" id="menu_degree_day" action="show_degree_day"/>
</data>
</openerp>
Upvotes: 3
Views: 1317
Reputation: 416
OpenERP expect the onchange function to return the data in the following format
{
'value' : {
'<field_x>': <value for field x>,
'<field_y>': <value for field y>,
}
'context' : <Context dict>
'domain' : {
'field_a' : <domain filter for field_a>,
'field_b' : <domain filter for fied_b>,
}
'warning' : {
'title': '<warning message title>',
'message': '<Warning message>'
}
}
context, domain, warning are optional. Value is mandatory and you can include the updated value for zero or more fileds in that vale dict.
In your case, at end of your onchange function, you may add
return {'value':{'heat_degree_day':temp,'hw_degree_day':temp + 5}}
Upvotes: 3