我不明白为什么函数不会在python中返回,我的函数会计算两个日期之间的天数(星期五和星期六),但不会返回
这是我的代码:
# -*- coding: utf-8 -*-
import datetime
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
from odoo import models, fields, api
class HrMission(models.Model):
_name = "hr.employee.mission"
_description = "hr mission"
_inherit = "hr.employee.mission"
days_compensation =fields.Float(compute='get_compensation', compstring='Jours de récupération', help="Jours de récupération si la mission contient les jours de repos",
required=True, readonly=True,)
@api.multi
@api.depends('mission_start_date', 'mission_end_date')
def get_compensation(self):
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
week = {}
leave_value = {}
# Compute Number Of Friday And Saturday
for i in range((time2 - time1).days):
day = calendar.day_name[(time1 + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
fri = week.get('Friday') if 'Friday' in week else 0 # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
friandsat = fri + sat # Result 2
return friandsat
答案 0 :(得分:1)
您需要在该字段上分配值。例如, rec.days_compensation = float_number
@api.multi
@api.depends('mission_start_date', 'mission_end_date')
def get_compensation(self):
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
week = {}
leave_value = {}
# Compute Number Of Friday And Saturday
for i in range((time2 - time1).days):
day = calendar.day_name[(time1 + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
fri = week.get('Friday') if 'Friday' in week else 0 # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
friandsat = fri + sat # Result 2
rec.days_compensation = friandsat
答案 1 :(得分:0)
您必须在for循环之外在函数的开头和结尾定义friandsat
:
def get_compensation(self):
friandsat = 0
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
week = {}
leave_value = {}
# Compute Number Of Friday And Saturday
for i in range((time2 - time1).days):
day = calendar.day_name[(time1 + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
fri = week.get('Friday') if 'Friday' in week else 0 # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
friandsat = fri + sat # Result 2
return friandsat
答案 2 :(得分:0)
更新代码
@api.depends('mission_start_date', 'mission_end_date')
def get_compensation(self):
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
week = {}
leave_value = {}
# Compute Number Of Friday And Saturday
for i in range((time2 - time1).days):
day = calendar.day_name[(time1 + datetime.timedelta(days=i + 1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
fri = week.get(
'Friday') if 'Friday' in week else 0 # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
rec.days_compensation = fri + sat # Result 2