3

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

1 回答 1

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>'
             }
}

上下文、域、警告是可选的。值是强制性的,您可以在该 vale 字典中包含零个或多个字段的更新值。

在您的情况下,在 onchange 函数结束时,您可以添加

return {'value':{'heat_degree_day':temp,'hw_degree_day':temp + 5}}
于 2013-06-11T05:29:42.693 回答