Openerp - 如何用write()更新字段

时间:2016-08-17 15:07:15

标签: python python-2.7 openerp openerp-7

我有这个功能,直到最后更新查询它工作正常。我尝试做的是通过添加0.5来更新'number_of_days_temp'字段的当前值。但cr.execute不会更新字段,只返回相同的旧值。请修复cr.executewrite()函数来帮助我。

我的功能

def allocate_on_probations(self, cr, uid, ids,tl=False, context=None):
    result = {}

    emps=self.pool.get('hr.employee').search(cr, uid, [('current_status','=','active')], context=context)
    if emps:

        for r in emps:

            hol_state=2 
            gt_dt=cr.execute("""SELECT appointed_date FROM hr_employee WHERE id= %d order by id"""%(r))
            gt_dd=cr.fetchone()[0]

            #getting today details
            today = datetime.datetime.now()
            tt=today.date()
            td=tt.day
            tm=tt.month
            ty=tt.year

            #getting appointment date details
            app=datetime.datetime.strptime(gt_dd, "%Y-%m-%d").date()
            ay=app.year
            am=app.month
            ad=app.day

            if ay==ty:
                #compairing today and appointed date
                comp=(tt-app)
                chat=int(comp.days)
                print chat
                chat_mod=chat%30
                print chat_mod
                print r

                if chat_mod==29:

                    hol_obj=self.pool.get('hr.holidays')
                    print hol_obj
                    condition_1=[('employee_id','=',r),('type','=','add'),('holiday_status_id','=',hol_state)]
                    hol_emp=hol_obj.search(cr, uid,condition_1, context=context)

                    if hol_emp:

                        for n in hol_emp:
                            print n

                            hol_tp = self.pool.get('hr.holidays').search(cr, uid, [('id','=',n)], context=None)
                            hol_tp_br = self.pool.get('hr.holidays').browse(cr, uid, hol_tp, context=None)
                            hol_name = hol_tp_br[0].number_of_days_temp
                            print hol_name

                            hol_name=(hol_name+0.5)
                            print hol_name

                            #This is where it dosn't up date the field and up to this the function gives the right values
                            #debug mode and as in here it prints the correct values.
                            cr.execute("""UPDATE hr_holidays SET number_of_days_temp= %d WHERE id= %d"""%(hol_inc,n))
                            cr.execute("""UPDATE hr_holidays SET number_of_days= %d  WHERE id= %d"""%(hol_inc,n))

    return True

1 个答案:

答案 0 :(得分:1)

以下是您方法的小优化版本

def allocate_on_probations(self, cr, uid, ids, tl=False, context=None):
    result = {}
    emp_obj = self.pool.get('hr.employee')
    emp_ids = emp_obj.search(cr, uid, [('current_status','=','active')], context=context)
    if emps:

        for emp in emp_obj.browse(cr, uid, emp_ids, context=context):
            hol_state=2
            gt_dt=cr.execute("""SELECT appointed_date FROM hr_employee WHERE id= %d order by id"""%(r))
            gt_dd=cr.fetchone()[0]

            #getting today details
            today = datetime.datetime.now()
            tt=today.date()
            td=tt.day
            tm=tt.month
            ty=tt.year

            #getting appointment date details
            app=datetime.datetime.strptime(gt_dd, "%Y-%m-%d").date()
            ay=app.year
            am=app.month
            ad=app.day

            if ay==ty:
                #compairing today and appointed date
                comp=(tt-app)
                chat=int(comp.days)
                chat_mod=chat%30

                if chat_mod==29:
                    hol_obj=self.pool.get('hr.holidays')
                    condition_1=[('employee_id','=',r),('type','=','add'),('holiday_status_id','=',hol_state)]
                    hol_emp=hol_obj.search(cr, uid,condition_1, context=context)
                    if hol_emp:
                        for holiday_rec in hol_obj.browse(cr, uid, hol_emp, context=context):
                            hol_name = holiday_rec.number_of_days_temp
                            hol_name_add =(hol_name+0.5)
                            hol_obj.write(cr, uid, [holiday_rec.id], {'number_of_days_temp': hol_name_add , 'number_of_days': hol_name} )
    return True

您必须查看写入并需要更改变量hol_name_addhol_name

希望这有帮助!