我发现openerp 7的文档很令人沮丧。
我试图从on_change事件调用一个函数来执行计算,并将结果放到同一行中的另一个字段中。
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
&LT;&GT;
<?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>
答案 0 :(得分:3)
OpenERP期望onchange函数以下列格式返回数据
{
'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>'
}
}
上下文,域名,警告是可选的。值是必需的,您可以在该值字典中包含零个或多个文件的更新值。
在您的情况下,在onchange函数结束时,您可以添加
return {'value':{'heat_degree_day':temp,'hw_degree_day':temp + 5}}