0

这是第一个代码
类 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 ','主题')
}
timetable_timetable()

这是第二个代码
类 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('Start Date',store=True),
           'duration' : fields.float('Duration',size=64,required=True) ,
           'end_date' : fields.datetime('End Date',store=True),
           'professor' : fields.many2one('professor.professor','Professor')
           }
timetable_subjects()

我想创建一个函数 on_change,它从 start_date 和持续时间返回 end_date。那么我该如何实现该功能。

4

2 回答 2

0

在end_dateonchange的方法中编写此代码,

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)几年,同样的方式是几小时或几分钟或几秒钟

于 2013-03-25T10:22:04.593 回答
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}
于 2013-03-25T10:33:31.633 回答