Python: ошибка при использовании strptime и timedelta

Я пытаюсь рассчитать дату платежа. Здесь код:

from datetime import datetime,timedelta

 commande = self.pool.get('commandes').browse(cr, uid,commande_id,context=context)
 date_commande= datetime.strptime(commande.date_commande, "%Y-%m-%d").date()
 res['due_date']=date_commande+timedelta(days=20)

поле due_date - тип даты

Я получаю эту ошибку:

XmlHttpRequestError INTERNAL SERVER ERROR

<title>500 Internal Server Error</title>
<h2>Internal Server Error</h2>
<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>

Я протестировал код без timedelta, и я все равно получил ту же ошибку

трассировка:

Traceback (most recent call last):
 File "werkzeug\serving.py", line 159, in run_wsgi 
 File "werkzeug\serving.py", line 146, in execute 
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\service\wsgi_server.py", line 417, in application
 return application_unproxied(environ, start_response)
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\service\wsgi_server.py", line 403, in application_unproxied
 result = handler(environ, start_response)
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 528, in __call__
 return self.dispatch(environ, start_response)
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 487, in __call__
 return self.app(environ, start_wrapped)
 File "werkzeug\wsgi.py", line 411, in __call__ 
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 487, in __call__
 return self.app(environ, start_wrapped)
 File "werkzeug\wsgi.py", line 411, in __call__ 
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 487, in __call__
 return self.app(environ, start_wrapped)
 File "werkzeug\wsgi.py", line 411, in __call__ 
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 553, in dispatch
 result = handler(request)
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 618, in <lambda>
 return lambda request: JsonRequest(request).dispatch(method)
 File "C:\Program Files (x86)\OpenERP 7.0-20140120-002508\Server\server\openerp\addons\web\http.py", line 251, in dispatch
 body = simplejson.dumps(response)
 File "simplejson\__init__.py", line 286, in dumps 
 File "simplejson\encoder.py", line 228, in encode 
 File "simplejson\encoder.py", line 515, in _iterencode 
 File "simplejson\encoder.py", line 483, in _iterencode_dict 
 File "simplejson\encoder.py", line 483, in _iterencode_dict 
 File "simplejson\encoder.py", line 483, in _iterencode_dict 
 File "simplejson\encoder.py", line 525, in _iterencode 
 File "simplejson\encoder.py", line 202, in default 
TypeError: datetime.date(2014, 3, 3) is not JSON serializable
</lambda>

Может кто-нибудь скажет мне, где я ошибся? благодаря

1 ответ

Ваша проблема в кодировании ответа на JSON, поскольку он не может разобрать дату и время (дата) на JSON, вы должны преобразовать его в строку, используя функцию strftime.

поэтому вы можете попробовать:

from datetime import datetime,timedelta

 commande = self.pool.get('commandes').browse(cr, uid,commande_id,context=context)
 date_commande= datetime.strptime(commande.date_commande, "%Y-%m-%d").date()
 res['due_date']= datetime.strftime(date_commande+timedelta(days=20),"%Y-%m-%d")

licensed under cc by-sa 3.0 with attribution.