我发现 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
<>
<?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>