Skip to Content
Menu
This question has been flagged
1 Reply
6751 Views


I made a method for an on_change method and here is my method for it.

    def onchange_date_start(self, cr, uid, ids, date_start, context=None):
factor = 2
date_1 = datetime.strptime(date_start, '%Y-%m-%d')
date2 = date_1.date()
end_date = datetime(date2.year,date2.month,date2.day) + relativedelta(months=+factor)
e_date = end_date
_logger.info("\n\t\t\t\tEND Date Final %s"%(str(e_date.date())))
value = {"value":{"end_on" : e_date.date()}}
return value

 

So far it print out my **e_date** correctly but an error also pop saying:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<h1>Internal Server Erro</h1>
<h1>Internal Server Error&lt</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

and here is what the terminal says:

        raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.date(2015, 10, 18) is not JSON serializable

What error i committed?

Thanks for any help/suggestion

Avatar
Discard
Best Answer

 you should strftime back to the string a date, after you calculate it.

Try this (you do too mush work):

 def onchange_date_start(self, cr, uid, ids, date_start, context=None):
        factor = 2
        date = datetime.strptime(date_start, '%Y-%m-%d') + relativedelta(months=factor)
return {"value":{"end_on" : date.strftime('%Y-%m-%d')}


OR in terms of your code:

 def onchange_date_start(self, cr, uid, ids, date_start, context=None):
        factor = 2
        date_1 = datetime.strptime(date_start, '%Y-%m-%d') 
        date2 = date_1.date() 
        end_date = datetime(date2.year,date2.month,date2.day) + relativedelta(months=+factor) 
        e_date = end_date 
        _logger.info("\n\t\t\t\tEND Date Final %s"%(str(e_date.date()))) 
        value = {"value":{"end_on" : e_date.strftime('%Y-%m-%d')}} 
        return value

Avatar
Discard
Author

@Temur , Thanks it works.

Author

and also for making it shorter. :)

You're welcome

Related Posts Replies Views Activity
1
Jul 23
1865
4
Dec 22
7962
2
Apr 21
2256
0
Jan 21
11390
2
Dec 19
8029