我有heat_degree_day和hw_degree_day设置为只读,因此用户无法直接修改它们。
我有一个on_change事件,它根据用户输入的其他字段修改它们。
当用户输入当天的高温和低温时,正确修改度数日字段;但是,当我单击“保存”按钮时,度数日字段将恢复为原始值。
我假设这是因为我将它们设置为仅在视图中读取。
我认为必须有一种常用的解决方法,但我没有在文档中找到它。
degree_day.py
from openerp.osv import osv, fields
from dateutil.parser import *
from dateutil.tz import *
from datetime import *
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('Hot Water Degree Day' ),
'debug':fields.text('Debug text'),
}
def write(self, cr, uid, ids, vals, context=None):
dd_recs = self.pool.get('degree.day')
low_temp = dd_recs.read(cr, uid, ids[0], ["low_temp"])["low_temp"]
high_temp = dd_recs.read(cr, uid, ids[0], ["high_temp"])["high_temp"]
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
a = 0
vals['heat_degree_day'] = a
vals['hw_degree_day'] = a + 5
return super(degree_day, self).write(cr, uid, ids, vals, context=context)
def generate_degree_day(self, cr, uid, ids, high_temp, low_temp, date, debug, context=None):
""" calculates heat degree day and hot water degree day when high or low temp is changed
@param high_temp: The day's high temperature
@param low_temp: The day's low temperature
"""
v={}
debug = "... "
# check to see that args are not null
if (high_temp and low_temp and date):
debug += "args present:"
adate = parse(date)
day_before = adate - timedelta(days=1)
dd_recs = self.pool.get('degree.day')
# were any degree.day records retrieved?
if dd_recs:
debug += " dd_recs not null:"
ids = dd_recs.search(cr, uid, [("date", "=", day_before)])
# is there a record for yesterday?
if ids:
debug += " ids not null:"
last_hdd = dd_recs.read(cr, uid, ids[0], ["heat_degree_day"])
last_hwdd = dd_recs.read(cr, uid, ids[0], ["hw_degree_day"])
# degree day calculation
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
debug += " a<0:"
a = 0
v['heat_degree_day'] = last_hdd["heat_degree_day"] + a
v['hw_degree_day'] = last_hwdd["hw_degree_day"] + a + 5
v['debug'] = debug
return {'value':v}
degree_day_view.xml
<?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">
<field name="date" />
<field name="high_temp" />
<field name="low_temp" />
<field name="heat_degree_day" />
<field name="hw_degree_day" />
<field name="debug" />
</tree>
</field>
</record>
<record id="degree_day_form" model="ir.ui.view">
<field name="name">degree.day.form</field>
<field name="model">degree.day</field>
<field name="arch" type="xml">
<form string="Degree Day" version="7.0">
<sheet string="Degree Day">
<h1>Day <field name="date" class="oe_inline"/></h1>
<group>
<field name="low_temp" on_change="generate_degree_day(high_temp, low_temp, date, debug)" />
<field name="high_temp" on_change="generate_degree_day(high_temp, low_temp, date, debug)" />
</group>
<group>
<field name="heat_degree_day" readonly="True" />
<field name="hw_degree_day" readonly="True" />
</group>
<field name="debug" />
</sheet>
</form>
</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 :(得分:0)
我们必须通过readonly reason在“write method”中写两个字段,只需在你的类中使用这个方法:
def write(self, cr, uid, ids, vals, context=None):
if vals.has_key('high_temp') and vals['low_temp']:
a = (high_temp + low_temp)/2 - 65
if a < 0:
a = 0
vals['heat_degree_day'] = a
vals['hw_degree_day'] = a + 5
return super(degree_day, self).write(cr, uid, ids, vals, context=context)
答案 1 :(得分:0)
我修改了Anup提交的代码,如下所示。这很有效。如果您认为我这样做是不恰当的,请告诉我。编辑:这是最终版本,并添加.create的等效代码以涵盖新记录
def write(self, cr, uid, ids, vals, context=None):
if vals.has_key('high_temp') and vals.has_key('low_temp'):
high_temp = vals['high_temp']
low_temp = vals['low_temp']
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
a = 0
vals['heat_degree_day'] = a
vals['hw_degree_day'] = a + 5
return super(degree_day, self).write(cr, uid, ids, vals, context=context)
def create(self, cr, uid, vals, context=None):
if vals.has_key('high_temp') and vals.has_key('low_temp'):
high_temp = vals['high_temp']
low_temp = vals['low_temp']
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
a = 0
vals['heat_degree_day'] = a
vals['hw_degree_day'] = a + 5
return super(degree_day, self).create(cr, uid, vals, context=context)