Reputation: 1382
I wan't to add to planned date integer. It's always will be from 1 to 10, but having problem with my code.
planned_date = fields.Date(string='Planned Date', required=False,
default=fields.Date.today)
def linedate(self):
if line.discount > 5:
daysz = line.product_id.seller_ids[0].delay # it's integer from 1 to 10 always
planned = (line.planned_date + timedelta(days=daysz) ).strftime('%Y-%m-%d')
line.planned_date = planned
print line.planned_date
I'm getting this kind of error
planned = (line.planned_date + timedelta(days=daysz) ).strftime('%Y-%m-%d')
TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found
Upvotes: 1
Views: 2370
Reputation: 26678
You need to convert planned_date
because Odoo stores dates as strings ans it would be better to use Date methods to convert from string and to Date objects, You can refer to Odoo fields.
static from_string(value): Convert an ORM value into a date value.
static to_string(value): Convert a date value into the format expected by the ORM.
def linedate(self):
if line.discount > 5:
daysz = line.product_id.seller_ids[0].delay # it's integer from 1 to 10 always
planned = fields.Date.from_string(line.planned_date) + timedelta(days=daysz)
line.planned_date = fields.Date.to_string(planned)
print line.planned_date
Upvotes: 2
Reputation: 12005
Install dateutil
(pip install python-dateutil
) and then you can easily parse line.planned_date
to datetime object
from dateutil import parser
planned = (parser.parse(line.planned_date) + timedelta(days=daysz) ).strftime('%Y-%m-%d')
Upvotes: 1
Reputation: 82755
line.planned_date
is a Unicode string object. You need to convert it to a datetime object and then add using timedelta.
Ex:
import datetime
planned = (datetime.datetime.strptime(line.planned_date, '%Y-%m-%d') + datetime.timedelta(days=daysz) ).strftime('%Y-%m-%d')
Upvotes: 3