콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
4 답글
1995 화면

Hi,

I am newbie in python and odoo. Please help me.

Below is my code:

work_type = fields.Selection([('normal', 'Normal')
                                   ,('overtime', 'Overtime'), ('holiday', 'Holiday Overtime')],'Work Type')

overtimes_hours = fields.Float(compute='onchange_overtimes', string='Overtime Hours',store=True)

@api.depends('timesheet_ids')
      def onchange_overtimes(self):
          for record in self:
              total = 0.0
              if self.timesheet_ids:
                 for line in self.timesheet_ids:
                    
                     total += line.unit_amount
              self.overtimes_hours = total


timesheet_ids is my one2many field. I added work_type as a selection field into this one2many field.

Each time i add a new record i select work_type, if the work_type = overtime. i want to call the above function.

For ex: first overtimes_hours = 0. On each addition of record (work_type = overtime). Each time value added to the existing value. After 1 week we will get the total overtime working hours seperately. But my code is not working properly.

Please correct me.


아바타
취소
작성자

first time for loop working. Next time it shows Singleton error. I think error in for loop. Please correct me.

Updated code, please check self.overtimes_hours change into record.overtimes_hours

베스트 답변

Functional calculate based on work_type field?

In that case depends also add work_type.

And also code updated, please check below code.

@api.depends('work_type', 'timesheet_ids')
      def onchange_overtimes(self):
          for record in self:
              total = 0.0
              if  record.work_type: 
                 for line in record.timesheet_ids:
                    
                     total += line.unit_amount
              record.overtimes_hours = total

아바타
취소