功能从startdate&获得enddate在openerp的持续时间

时间:2013-03-25 09:56:45

标签: openerp

这是第一个代码
class timetable_timetable(osv.osv):
_name = 'timetable.timetable'
_columns = {
'name':fields.char('Name',size =“64”,required = True),
'course':fields.many2one('college.course','Course'),
'semester':fields.many2one('course.semester','Semester'),
'subject':fields.one2many('timetable.subjects','timetable_id','Subject')
            }
timetable_timetable()

这是第二个代码
class timetable_subjects(osv.osv):
_name ='timetable.subjects'
_columns = {
'timetable_id':fields.many2one('timetable.timetable','Time Table'),
'subject_id':fields.many2one('semester.subject','Subject'),
'start_date':fields.datetime('开始日期',存储=真),
'duration':fields.float('Duration',size = 64,required = True),
'end_date':fields.datetime('结束日期',存储=真),
'教授':fields.many2one('professor.professor','教授')
 }
timetable_subjects()

我想创建一个on_change函数,它从start_date&中返回end_date。持续时间。那我怎么能做到这个功能呢。

2 个答案:

答案 0 :(得分:0)

end_date onchange方法中写下此代码,

from datetime import datetime
from dateutil.relativedelta import relativedelta

def my_onchange(self, cr, uid, ids, s_date, duration):
    return {'value': {'end_date': (datetime.strptime(s_date,'%Y-%m-%d %H:%M:%S') + relativedelta(days=duration)).strftime('%Y-%m-%d %H:%M:%S')}}

如果您的持续时间以月或年为单位,则数月relativedelta(months=duration)relativedelta(years=duration)年数相同,小时或分钟或秒相同。

答案 1 :(得分:0)

def on_change(self,cr,uid,ids,t_date,duration):

    res = {'end_date': False}
    if t_date:
        now = datetime.strptime(t_date, '%Y-%m-%d %H:%M:%S')

        e_date = now + timedelta(hours=duration)
        print e_date
        f_date = datetime.strftime(e_date, '%Y-%m-%d %H:%M:%S')
        res['end_date'] = f_date
    return {'value': res}