Python 2.7 打开时只读字段不写入数据库\u更改事件修改它

Python 2.7 打开时只读字段不写入数据库\u更改事件修改它,python-2.7,openerp,Python 2.7,Openerp,我已经将热度日和热度日设置为只读,所以用户不能直接修改它们 我有一个on_change事件,根据用户输入的其他字段修改它们 当用户输入当天的高、低温度时,度数日字段被正确修改;但是,当我单击“保存”按钮时,“度数日”字段将恢复为其原始值 我假设这是因为我在视图中将它们设置为只读 我认为必须有一种通用的方法来解决这个问题,但我在文档中找不到它 学位日 from openerp.osv import osv, fields from dateutil.parser import * from dat

我已经将热度日和热度日设置为只读,所以用户不能直接修改它们

我有一个on_change事件,根据用户输入的其他字段修改它们

当用户输入当天的高、低温度时,度数日字段被正确修改;但是,当我单击“保存”按钮时,“度数日”字段将恢复为其原始值

我假设这是因为我在视图中将它们设置为只读

我认为必须有一种通用的方法来解决这个问题,但我在文档中找不到它

学位日

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}
从openerp.osv导入osv,字段
从dateutil.parser导入*
从dateutil.tz导入*
从日期时间导入*
班级学位日(osv.osv):
_name=“学位日”
_纵队={
“日期”:字段。日期(“日期”),
“高温”:字段.integer(“高温”),
“低温”:字段.integer(“低温”),
“热度日”:fields.integer(“热度日”),
'hw_degree_day':fields.integer('Hot Water degree day'),
“调试”:fields.text(“调试文本”),
}
def写入(自身、cr、uid、ID、VAL、上下文=无):
dd_recs=self.pool.get('degree.day'))
低温=数据记录读取(cr、uid、ID[0],“低温”])[“低温”]
高温=数据记录读取(cr、uid、ID[0],“高温”])[“高温”]
a=65-((高温+低温)/2)
如果a<0:
a=0
VAL[‘热量(度)日]=a
VAL['hw_degree_day']=a+5
返回super(degree\u day,self).write(cr,uid,ids,VAL,context=context)
def生成日期(自身、cr、uid、ID、高温、低温、日期、调试、上下文=无):
“”“计算高温或低温改变时的热度日和热水度日
@参数高温:当天的高温
@参数低温:一天的低温
"""
v={}
debug=“…”
#检查参数是否不为空
如果(高温、低温和日期):
调试+=“存在参数:”
adate=parse(日期)
前一天=adate-时间增量(天=1)
dd_recs=self.pool.get('degree.day'))
#是否检索到学位日记录?
如果dd_记录:
调试+=“dd_记录不为空:”
ids=dd记录搜索(cr,uid,[(“日期”,“前一天)])
#昨天有唱片吗?
如果ID为:
调试+=“ID不为空:”
最后一个硬盘驱动器=dd记录读取(cr、uid、ID[0],“热度日”])
last_hwdd=dd记录读取(cr、uid、ID[0],“hw学位日”])
#学位日计算
a=65-((高温+低温)/2)
如果a<0:

debug+=“a由于只读原因,我们必须在“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)
def写入(self、cr、uid、id、vals、context=None):
如果VAL.具有键(“高温”)和VAL[“低温”]:
a=(高温+低温)/2-65
如果a<0:
a=0
VAL[‘热量(度)日]=a
VAL['hw_degree_day']=a+5
返回super(degree\u day,self).write(cr,uid,ids,VAL,context=context)

我修改了Anup提交的代码,如下所示。这很有效。如果您认为我这样做不合适,请告诉我。编辑:这是最终版本,还为添加了等效代码。创建以覆盖新记录

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)
def写入(self、cr、uid、id、vals、context=None):
如果VAL.有按键(“高温”)且VAL.有按键(“低温”):
高温=VAL[“高温”]
低温=VAL[“低温”]
a=65-((高温+低温)/2)
如果a<0:
a=0
VAL[‘热量(度)日]=a
VAL['hw_degree_day']=a+5
返回super(degree\u day,self).write(cr,uid,ids,VAL,context=context)
def创建(自身、cr、uid、VAL、上下文=无):
如果VAL.有按键(“高温”)且VAL.有按键(“低温”):
高温=VAL[“高温”]
低温=VAL[“低温”]
a=65-((高温+低温)/2)
如果a<0:
a=0
VAL[‘热量(度)日]=a
VAL['hw_degree_day']=a+5
返回super(degree\u day,self).create(cr,uid,VAL,context=context)

我添加了您建议的代码,并进行了一些编辑以消除语法错误(请参见上面的清单),但heat_degree_day和hw_degree_day的值仍然没有写入数据库。我甚至尝试在generate_degree_day结束时直接写入记录,但仍然不起作用。可能在到达此写入例程之前拦截了这些值……也许我需要从记录本身而不是从如果他们在VAL争论中…我会试试这个。
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)