python - 类型错误:+ 不支持的操作数类型:“NoneType”和“NoneType”
问题描述
我是 Python 的新用户,我的None
类型有问题,我看了不同的问题,但问题仍然存在。
我的代码正在寻找两个日期之间的星期五和星期六,到目前为止它有效,但是当我将两者相加时,我遇到了这个错误:
“TypeError:+ 的不支持的操作数类型:‘NoneType’和‘NoneType’
在我在函数中返回结果之后。
这是我的代码:
# -*- 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') # Result Number 4 Of Friday If "Start Date", "End date" --> "01/01/2017", "31/01/2017"
sat = week.get('Saturday') # Same thing that friday
friandsat = mon + sat # Error ---> "TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'"
rec.days_compensation = friandsat
解决方案
- 需要注意的一件事是,开始日期和结束日期是否都包含在日期范围内。例如,如果两个日期都是
datetime.datetime(2019, 3, 1)
,那么答案应该是1
吗?
...
def get_compensation(self):
def date_range_inclusive(rec):
# if either date is undefined, then so is date range
if not rec.mission_end_date or not rec.mission_start_date:
return iter([])
# Add 1 for inclusivity of both start and end dates
num_days = abs((rec.mission_end_date - rec.mission_start_date).days) + 1
return (rec.mission_start_date + timedelta(days=n) for n in range(num_days))
for rec in self:
# date range, including both start and end dates
dates = date_range_inclusive(rec)
# desired days to count
day_selection = (calendar.FRIDAY, calendar.SATURDAY)
rec.days_compensation = sum([dt.weekday() in day_selection for dt in dates])
上面的代码将假定日期范围包括在内。删除+ 1
以使其不包括在内。days_compensation
如果不包括周五或周六,它将将该字段设置为 0。
请注意,无需在数据类型之间来回转换,因为日历日枚举和
weekday
函数在datetime
评估中都可以比较整数。要记住的另一件事是,根据https://www.odoo.com/documentation/8.0/reference/orm.html
computed fields are not stored by default, they are computed and returned when requested. Setting store=True will store them in the database and automatically enable searching
因此,您可能想要:
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,
store=True)
请注意,唯一的更新是store=True
推荐阅读
- javascript - 如何制作一个按钮,将用户带到另一个网站上的特定位置
- css - Lighthouse:确保在加载 MaterialDesignIcons Webfonts 期间文本保持可见
- android - 如何创建一个联结查询,从联结本身返回一个值?
- javascript - 按钮点击和 2 个网页
- rust - Rust 疯狂指针 ref
- python - Ultra96 或 Pynq-Z2 套件?
- android - 试图在 android 中创建 spinner.OnItemSelectedListener 事件
- sed - sed 替换或删除字符串
- laravel - 如何自定义重置密码链接
- javascript - WordPress 编辑器在复制和粘贴时如何保留文本格式?