首页 > 解决方案 > 类型错误:+ 不支持的操作数类型:“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

标签: pythonodoononetype

解决方案


  • 需要注意的一件事是,开始日期和结束日期是否包含在日期范围内。例如,如果两个日期都是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。

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


推荐阅读