Type "help", "copyright", "credits" or "license" for more information.
>>> from odoo import models, fields, api
>>> t=fields.Date.today()
>>> t
datetime.date(2021, 12, 19)
>>> t.day
19
>>>
>>> import datetime
>>> y=t+10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'
>>> y=t+datetime.timedelta(+2)
>>> y
datetime.date(2021, 12, 21)
>>> y-t
datetime.timedelta(days=2)
>>> (y-t).days
2
for record in self: time_count = timedelta(record.target_date-record.jh_start_date).days if fields.Date.today() > record.jh_start_date: time_count_yy = timedelta(fields.Date.today()-record.jh_start_date).days else: time_count_yy = 0 if time_count > 0: record.progress_time = (time_count_yy / time_count) * 100 else: record.progress_time = 0.0
以上代码会出现:
TypeError: unsupported type for timedelta days component: datetime.timedelta
应写为:
for record in self: time_count = (record.target_date-record.jh_start_date).days if fields.Date.today() > record.jh_start_date: time_count_yy = (fields.Date.today()-record.jh_start_date).days else: time_count_yy = 0 if time_count > 0: record.progress_time = (time_count_yy / time_count) * 100 else: record.progress_time = 0.0