有条件地插入几个小时

时间:2019-06-25 01:37:26

标签: python odoo

晚上好,请问您能如何帮助我解决这个问题,以便出现一条消息说您不能花一个小时,因为它已经很忙了?,我目前有:

class reserva (models.Model):

     _name='gimnasio.reserva'

     tipo_reserva=fields.Selection([('clase','Clase'),('evaluacion','Evaluacion')])

     fecha_reserva=fields.Date()

     start_time=fields.Float()

     end_time=fields.Float()

     def fecha(self):

          if self.star_time==self.star_time:

               raise validationError('the hour is busy')

2 个答案:

答案 0 :(得分:1)

我认为您可以在datetime模块中使用strptime方法。

from datetime import datetime as dt
    start_time = fields.Float()
    end_time = fields.Float()

    @api.onchange('start_time','end_time')
    def _check(self):
        records = self.env["gimnasio.reserva"].search([("day", '=', the day you want to check eg. "2019-06-13")])
        for rec in records:
            ref_start = dt.strptime(str(rec.start_time), "%H:%M")
            curr_start = dt.strptime(str(self.start_time), "%H:%M")
            if ref_start == curr_start:
                raise validationError('the hour is busy')

我还没有调试,您可以尝试。

how to eliminate the default date that you added ("2019-06-13") and that any date should not have the same busy schedule?

在这种情况下,您不需要日期时间模块

    @api.constrains("start_time")
    def _check(self):
        # search db for any record have same start time.
        records = self.env["gimnasio.reserva"].search([('start_time ','=', self.start_time)])
        if len(records) > 0:
            raise validationError('the hour is busy')

答案 1 :(得分:1)

I have another question for you. you know how to configure Datetime only for hour and minutes because I only need hour and minutes but not the date.

仅将日期时间配置为小时和分钟。


time = fields.Datetime("time")
custom_time = fields.Char('my_custome_time')
    @api.onchange('time')
    def _get_time(self):
        if self.time:
            for rec in self:
                # datetime value is a string like 'YYYY-mm-dd HH:MM:SS' 
                # so just extract string from position 11 to 16
                _time = self.time[11:16]
                self.custom_time = _time
                rec.custom_time = self.custom_time