TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found ODOO10
TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found ODOO10
我正在使用 odoo 10,我想计算 arrival_time
我有两个字段,一个是浮点字段,一个是日期时间字段,我想要的是当用户在那个浮点字段中输入一个值时,它将被添加到那个到达时间
这是我的代码:
from odoo import models
from odoo import fields
from odoo import api
from datetime import datetime
class TransportManagementLines(models.Model):
_name = 'transport.management.lines'
_description = 'Transport Management Lines'
_rec_name = 'chauffeur'
chauffeur = fields.Many2one('transport.management.matricule',string='Nom Chauffeur', required="1",track_visibility='always')
starting_cities = fields.Many2one('transport.management.trajet',string='Trajet De Transport', required="1",track_visibility='onchange')
starting_time = fields.Datetime(string="Heure Départ", required="1",track_visibility='onchange')
arrival_time = fields.Datetime(string="Heure d'arriver", required="1", compute="set_arrival_time", store=True)
@api.depends('starting_cities','starting_time')
def set_arrival_time(self):
for rec in self:
if rec.starting_time and rec.starting_cities:
rec.arrival_time = rec.starting_time + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))
else:
rec.arrival_time = False
class TransportManagementTrajet(models.Model):
_name = 'transport.management.trajet'
_description = 'Transport Management Trajet'
_rec_name = 'nom_depart'
nom_depart = fields.Many2one('transport.management.config',string="Villes De Trajet", required="1")
travel_distance = fields.Integer(string='Nombre de KM', required="1")
travel_time = fields.Float(string="Durée de Trajet", required="1")```
Odoo 将 datetime 字段存储为字符串,因此在添加 timedelta 之前,您需要将其转换为 datetime 类型。
这应该有效:
rec.arrival_time = fields.Datetime.from_string(rec.starting_time) + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))
我正在使用 odoo 10,我想计算 arrival_time
我有两个字段,一个是浮点字段,一个是日期时间字段,我想要的是当用户在那个浮点字段中输入一个值时,它将被添加到那个到达时间
这是我的代码:
from odoo import models
from odoo import fields
from odoo import api
from datetime import datetime
class TransportManagementLines(models.Model):
_name = 'transport.management.lines'
_description = 'Transport Management Lines'
_rec_name = 'chauffeur'
chauffeur = fields.Many2one('transport.management.matricule',string='Nom Chauffeur', required="1",track_visibility='always')
starting_cities = fields.Many2one('transport.management.trajet',string='Trajet De Transport', required="1",track_visibility='onchange')
starting_time = fields.Datetime(string="Heure Départ", required="1",track_visibility='onchange')
arrival_time = fields.Datetime(string="Heure d'arriver", required="1", compute="set_arrival_time", store=True)
@api.depends('starting_cities','starting_time')
def set_arrival_time(self):
for rec in self:
if rec.starting_time and rec.starting_cities:
rec.arrival_time = rec.starting_time + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))
else:
rec.arrival_time = False
class TransportManagementTrajet(models.Model):
_name = 'transport.management.trajet'
_description = 'Transport Management Trajet'
_rec_name = 'nom_depart'
nom_depart = fields.Many2one('transport.management.config',string="Villes De Trajet", required="1")
travel_distance = fields.Integer(string='Nombre de KM', required="1")
travel_time = fields.Float(string="Durée de Trajet", required="1")```
Odoo 将 datetime 字段存储为字符串,因此在添加 timedelta 之前,您需要将其转换为 datetime 类型。
这应该有效:
rec.arrival_time = fields.Datetime.from_string(rec.starting_time) + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))