python – 我想以浮点值显示休假持续时间

我在hr_leave_rules.py中创建了half_day_allowed字段

from odoo import models,fields,api,_

class HRLeaveRules(models.Model):
    _name = 'hr_leave_rules.leave_rules'

    half_day_allowed = fields.Selection([
        ('yes',"Yes"),('no',"No")],string="Half Day Allowed",required=True)

而且,我继承了get_number_of_days字段,它们计算的休假时间是多少天,而holiday_status_id则表示休假类型.我想要做的是,如果对于特定的holiday_status_id,如果half_day_allowed为’yes’,则在get_number_of_days中它应该采用浮点值,否则它将采用整数值.为此,我尝试了下面的代码,但它没有工作.plz帮帮我.

leave_type.py

from odoo import fields,models,_
from math import ceil
from datetime import timedelta
from openerp.exceptions import UserError

HOURS_PER_DAY = 8

class leave(models.Model):
    _inherit = "hr.holidays"


    @api.onchange('number_of_days_temp')
    def _holiday_status_id(self):

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id','=',self.holiday_status_id.id)])

    @api.onchange('date_from')
    def _onchange_date_from(self):

        date_from = self.date_from
        date_to = self.date_to

        if date_from and not date_to:
            date_to_with_delta = fields.Datetime.from_string(date_from) + timedelta(hours=HOURS_PER_DAY)
            self.date_to = str(date_to_with_delta)
            current = self.env['hr_leave_rules.leave_rules'].search([(
                    'holiday_status_id',self.holiday_status_id.id)])

            if current.half_day_allowed == 'yes':
                if (date_to and date_from) and (date_from <= date_to):
                    self.number_of_days_temp = self._get_number_of_days(
                            date_from,date_to,self.employee_id.id)
                else:
                    self.number_of_days_temp = 0
            else:
                if (date_to and date_from) and (date_from <= date_to):
                        self.number_of_days_temp = ceil(self._get_number_of_days(
                            date_from,self.employee_id.id))
                else:
                    self.number_of_days_temp = 0

    @api.onchange('date_to')
    def _onchange_date_to(self):

        date_from = self.date_from
        date_to = self.date_to

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id',self.holiday_status_id.id)])

        if current.half_day_allowed == 'yes':
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = self._get_number_of_days(
                    date_from,self.employee_id.id)
            else:
                self.number_of_days_temp = 0
        else:
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = ceil(self._get_number_of_days(
                    date_from,self.employee_id.id))
            else:
                self.number_of_days_temp = 0

解决方法

从哪里开始……

首先,如果您有一个Yes / No的字段,则应将其定义为布尔字段,而不是Char字段. Fields Documentation.它将允许你做更简单的if / else逻辑,更不用说它只是一个更好的做法.

if half_day_allowed:
    # Do this way
else:
    # Do that way

其次,如果你有重复的代码,你应该将它分解为自己的方法以便于阅读. Don’t repeat yourself.

你重复了整整一节两次:

if half_day_allowed == 'yes':
    if (date_to and date_from) and (date_from <= date_to):
        self.number_of_days_temp = self._get_number_of_days(
                date_from,self.employee_id.id)
    else:
        self.number_of_days_temp = 0
else:
    if (date_to and date_from) and (date_from <= date_to):
            self.number_of_days_temp = ceil(self._get_number_of_days(
                date_from,self.employee_id.id))
    else:
        self.number_of_days_temp = 0

相反,只需创建一个方法并在需要时调用它.

@api.multi
def get_number_of_days_temp(self,half_day_allowed=False):
    self.ensure_one()
    if half_day_allowed == 'yes':
        # Do this
    else:
        # Do that

@api.onchange('date_from')
def _onchange_date_from(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

@api.onchange('date_to')
def _onchange_date_to(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

最后,为了确定未获得预期结果的问题,我们需要您提供更多信息.你到目前为止所说的只是:

I tried the below code but it is not working.

Its not showing correct get_number_of_days in both the cases i.e.,in ‘yes’ and ‘no’

这对确定问题没有帮助.有错误信息吗?它是否返回无效数据?如果是这样,那么输入和输出是什么以及期望它应该是什么?

在不知道任何进一步的情况下,我唯一可以猜测的是你的number_of_days_temp字段(你没有在你的问题中包含字段定义)被定义为一个整数字段,在这种情况下它总是会忽略小数.必须将它定义为Float才能使其工作.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...