新人的解答 : readonly 的字段 OE 是不會傳遞到后端的.
- 
字段设定为 
 'remaining_holidays': fields.float('Remaining of Annual Leave Days', readonly=True),
 但数值是由其它字段中的 onchange_type() 时所指定的.
 最後再用
 result['value'] = {'remaining_holidays': diff_day1 }
 目前操作上都正常..也看到指定後的數值..
 问题是 存档後.或workflow的流程的變化, 这个字段所記錄的數值都为 0,
 若把 readonly 取消. 一樣的操作 则存檔後所指定都是OK的..是有數值的
 但...这字段是不能调整的..是要由 on change 後計算而來的..
 重庆-mrshelly(49812643) 17:34:58
 是的. readonly 的字段 OE 是不會傳遞到后端的.
 要達到功能. 你需要在 write 方法里去設置 該字段的值, 即從你的 on change 里得到值后, 放在 write 方法的 vals 里面去.
 上海-就這樣(1683223795) 17:56:05
 想了一下, 是不是在 button 的变化时.
 def leaves_open(self, cr, uid, ids):
 self.write(cr, uid, ids, { 'state': 'open' ,'approve_date': time.strftime('%Y-%m-%d %H:%M:%S')})
 return True
 把值放入呢?
 上海-就這樣(1683223795) 17:57:07
 也就是在这个 def leaves_open 再写一定 on change 时的逻辑判断.
 再写一次.
 重庆-mrshelly(49812643) 17:58:02
 你可以在 open 的時候去寫一次.
 我推薦在 write 的時候寫.
- 
作法. 
 在 **_workflow.xml 中找出你要變更的 event.
 如 :
 <record model="workflow.activity" id="act_apply">
 <field name="wkf_id" ref="wkf_leaves" />
 <field name="name">apply</field>
 <field name="kind">function</field>
 <field name="action">leaves_apply(leave_type, date_from, date_to, time_from, time_to)</field>
 </record>
 在 xxx.py 中 加你的邏輯.
 def leaves_apply(self, cr, uid, ids, leave_type, date_from, date_to, time_from, time_to 
 getcurrent_leaveday = self.get_current_leaveday(date_from, date_to, time_from, time_to)
 self.write(cr, uid, ids, { 'state': 'apply' , 'leave_day' : getcurrent_leaveday , 'hr_approve_date': time.strftime('%Y-%m-%d %H:%M:%S')})
 if leave_type== 'C' :
 obj = self.pool.get('leaves_day')
 obj.create(cr,uid, {'leave_type':ids, 'employee_id': uid , 'leave_days' : getcurrent_leaveday , 'leave_active' : True})
 return True
 這樣 readonly 字段來源就會寫上了.. (我要寫入的字為 leave_day, leave_day 是由其它 副程式 return 回來的)
- 
 上海-就这样(1683223795) 13:53:39
 hr.holidays.py 中.
 def onchange_date_from(self, cr, uid, ids, date_to, date_from):
 result = {}
 if date_to and date_from:
 diff_day = self._get_number_of_days(date_from, date_to)
 result['value'] = {
 'number_of_days_temp': round(diff_day)+1
 }
 return result
 result['value'] = {
 'number_of_days_temp': 0,
 }
 return result
 其中 if date_to and date_from:
 这句话是什么用意..
 因我在使用时...不加这行..都有 exception 说 date_to 及 date_from 是 bool 的 datatype.
 strptime 只能接受 string .
 但 date_to 及 date_from 是从 字段中预设值就有的.(日期格式)..带入这 onchange_date_from 中.
 所以有点搞不太懂这用意...但没有这判断式一定会错..
 附上
 def _get_number_of_days(self, date_from, date_to):
 """Returns a float equals to the timedelta between two dates given as string."""
 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
 from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT)
 to_dt = datetime.datetime.strptime(date_to, DATETIME_FORMAT)
 timedelta = to_dt - from_dt
 diff_day = timedelta.days + float(timedelta.seconds) / 86400
 return diff_day
 重庆-mrshelly(49812643) 13:54:47
 那句是判断你倒底有没有选择日期.
 如果 起止日期都有选择时, 才执行 if 里面的内容.
 上海-Jeff(85822082) 13:57:02
 python中所有数据类型的空值都可以视为逻辑假
 非空值作为逻辑真
 openerp里这种判断方式很多很多