Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
16000 Widoki

Hello Everyone, 

I have to try to add month into today date .

my code below : 

from odoo.tools import float_is_zero, float_compare, DEFAULT_SERVER_DATETIME_FORMAT
from datetime import datetime, date, timedelta
import datetime
start_month = fields.Date("start")
end_month =fields.Date("end", compute='_calculate_month')

@api.depends('start_month')
@api.multi
def _calculate_month(self):
if self.start_month:
Date = datetime.datetime.now()
EndDate = Date + datetime.timedelta(months=6)
end_month = EndDate
print "CD:", Date, end_month

Thank you in Advanced 

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,

To add months with todays date, you can do it like this

from datetime import datetime
from dateutil.relativedelta import relativedelta

six_months_after  = datetime.now() + relativedelta(months=6)

or in your code change the line ,

Date = datetime.datetime.now()

into

Date = datetime.now()

For writing the values into field, you have to do like this

self.end_month = six_months_after

thanks

Awatar
Odrzuć
Autor

Hello, I am implement changes and it's value print but it's value is not reflected to my field. and changes code below :

start_month = fields.Date("start")

end_month =fields.Date("end", compute='_calculate_month')

@api.depends('start_month')

@api.multi

def _calculate_month(self):

if self.start_month:

Date_Start = datetime.now()

print"Start Date", Date_Start

six_months_after = Date_Start + relativedelta(months=6)

print"After six month", six_months_after

end_month = six_months_after

print "end month:", end_month

so anything wrong in above code?

Answer updated, please check it

Autor

Thanks @Niyas, it's work for me. But one more query how can i change datenow field with custom date field because it show error when i use default date instead of datenow field. Also i use duration in month(6, 12, 24,36).

Thanks for your reply

Autor Najlepsza odpowiedź

Hello, I am implement changes and it's value print but it's value is not reflected to my field. and changes code below :

start_month = fields.Date("start")

end_month =fields.Date("end", compute='_calculate_month')

@api.depends('start_month', '')

@api.multi

def _calculate_month(self):

if self.start_month:

Date_Start = datetime.now()

print"Start Date", Date_Start

six_months_after = Date_Start + relativedelta(months=6)

print"After six month", six_months_after

end_month = six_months_after

print "end month:", end_month

so anything wrong in above code?

Awatar
Odrzuć

Do like this,

self.end_month = six_months_after.

Updated the answer

Powiązane posty Odpowiedzi Widoki Czynność
1
lip 19
8432
1
sty 22
28756
1
cze 18
15234
1
mar 18
4074
0
mar 18
2515