Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to add 42 days to an date field?

By
wizardz
on 9/27/16, 5:01 AM 201 views

How can I add 42 days to an date-field?

I need help with that, I want to add 42days to the invouce_payed field and put it on a new date.field like:

pay_date_two

@api.onchange('invoice_payed')    
def _check_change(self):
 self.pay_date_two
# = self.invoice_payed
self.pay_date_two = self.pay_date_two + datetime.timedelta(days=42)

1
ayman mohammed adam
On 9/27/16, 6:55 AM

Dear Wizardz,

try this code

from dateutil.relativedelta import relativedelta

@api.onchange('invoice_payed')
def _check_change(self):
if self.invoice_payed:
       date_1= (datetime.strptime(self.invoice_payed, '%Y-%m-%d')+relativedelta(days =+ 42))

       self.pay_date =date_1

else:

        self.pay_date =False


I hope I helped you...

wow amazing. what was the problem with that bool? it's because invoice_payed comes as a bool back if it's emptry?

wizardz
on 9/27/16, 7:13 AM

Yes, I think that..

ayman mohammed adam
on 9/27/16, 7:32 AM
0
Yvan
On 9/27/16, 7:22 AM

Hi Wizardz,

if your date filed is empty (meaning you have not stored any value in it yet), the ORM will return False.

That's why, you must use the if self.invoice_payed to make sure the field contains a valid datetime value.


@api.onchange('invoice_payed') 
def _check_change(self):
if self.invoice_payed:
date_1 = datetime.datetime.strptime(self.invoice_payed, "%Y-%m-%d")
self.pay_date = date_1 + datetime.timedelta(days=42)

 

Best regards

Yvan

0
wizardz
On 9/27/16, 5:38 AM

I have fix it with that:

@api.onchange('invoice_payed')
 def _check_change(self):
date_1 = datetime.datetime.strptime(self.invoice_payed, "%Y-%m-%d")
 self.pay_date = date_1 + datetime.timedelta(days=42)


Now when I create a new record it gives me this error:

date_1 = datetime.datetime.strptime(self.invoice_payed, "%Y-%m-%d")

TypeError: must be string, not bool


self.invoice_payed , why is this a bool when I have a date field ?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/27/16, 5:01 AM
Seen: 201 times
Last updated: 9/27/16, 7:22 AM