Skip to Content
Menu
This question has been flagged
3 Replies
3665 Views

Hello

i am very new to odoo and  i am using the odoo 10 framework. i am presently trying to add number of days to current date in order to get expiry date. Below is my code.

class Payment(models.Model):

_inherit = 'account.voucher'

number_days = fields.Integer(string="Days")

effective_date = fields.Date(string="Effective Date") 

expiry_date = fields.Date(string="Expiry Date")

@api.one

@api.onchange("effective_date", "number_days")

def _onchange_expiry_date(self):

strftime = "%Y-%m-%d"

number_days = datetime.timedelta()

effective_date = datetime.date(self.effective_date,strftime)

expiry_date = datetime.date(self.expiry_date,strftime)

self.expiry_date = self.effective_date + timedelta(days=self.number_days)

I keep getting this error

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 641, in _handle_exception
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 683, in dispatch
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 333, in _call_function
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\service\model.py", line 101, in wrapper
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 326, in checked_call
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 941, in __call__
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\http.py", line 506, in response_wrap
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\addons\web\controllers\main.py", line 890, in call_kw
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\addons\web\controllers\main.py", line 882, in _call_kw
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\api.py", line 689, in call_kw
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\api.py", line 680, in call_kw_multi
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\models.py", line 5510, in onchange
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\models.py", line 5408, in _onchange_eval
  File "<decorator-gen-267>", line 2, in _onchange_expiry_date
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\api.py", line 369, in loop
  File "C:\Program Files (x86)\Odoo 10.0\server\odoo\addons\subscription\models\subscription.py", line 61, in _onchange_expiry_date
TypeError: an integer is required
Avatar
Discard
Best Answer

Hello , 

Try this one may be it's help you

import timedelta

current_date = datetime.datetime.now().date()
new_date = current_date + datetime.timedelta(days=7(numbers of days))
Avatar
Discard
Best Answer

Try adding logic to sort out type=none

I think the error is popping up because onchange happens at the time either one of the fields is being set to a datetime value.  

The other one is still null/none, therefore, 'self.effective_date' or 'self.expiry_date' is not what datetime.date() expects to see.

effective_date = datetime.date(self.effective_date,strftime)

expiry_date = datetime.date(self.expiry_date,strftime)

Avatar
Discard