Openerp - 如何使用 write() 更新字段
Openerp - how to update filed with write()
我有这个功能,直到上次更新查询都工作正常。我试图做的是通过添加 0.5 来更新 'number_of_days_temp' 字段的当前值。但是 cr.execute
不会更新该字段,只是 return 相同的旧值。请帮助我修复 cr.execute
或 write()
函数。
我的函数
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
这是你的方法的小优化版本
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
你要看write需要改变量hol_name_add
和hol_name
.
希望对您有所帮助!
我有这个功能,直到上次更新查询都工作正常。我试图做的是通过添加 0.5 来更新 'number_of_days_temp' 字段的当前值。但是 cr.execute
不会更新该字段,只是 return 相同的旧值。请帮助我修复 cr.execute
或 write()
函数。
我的函数
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
这是你的方法的小优化版本
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
你要看write需要改变量hol_name_add
和hol_name
.
希望对您有所帮助!