Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4671 Lượt xem

When I traying to subtract this :

def get_duration(self):
for rec in self:
if rec.notification:
if rec.notification == 'month':
rec.notification_date = self.date_end - fields.Date(date.today() + relativedelta(months=1))
print(type(self.date_end))
print(type(date.today() + relativedelta(months=1)))

I get this error unsupported operand type(s) for 'datetime.date' and 'Date'.




Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi jenan soliman,

You are trying to subtract between different data types. you can use following code for finding today's date. So both the datatypes will be in same format (fields.Date()). When you subtract two date objects, you will get the difference of those dates only. it will be a datetime.timedelta() object not a date object . So you have to find the date again using this timedelta object . See the code snippet below.


date_today = fields.Date.today()
date_difference = self.date_end - (date_today + relativedelta(months=1))

rec.notification_date = self.date_end - relativedelta(days=date_difference)

Thanks

Happy to help,

Ảnh đại diện
Huỷ bỏ
Tác giả

hi Muhsin,
I tried this, but I also get this error
'datetime.timedelta' object is not subscriptable.

Hi @jenan,
Sorry, I have updated the Answer with more details. please check once again, and don't forget to upvote if you find the answer usefull.

Thanks in advance

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 12 23
14675
0
thg 10 23
33
3
thg 10 23
788
1
thg 10 23
569
1
thg 8 23
2420