Question

I'am trying to calculate a due date. Here's the code:

 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)

the field due_date is date type

I get this error:

XmlHttpRequestError INTERNAL SERVER ERROR
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</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>

I tested the code without timedelta and i still got the same error

the traceback:

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

Please can someone tell me where i made a mistake? Thanks

Était-ce utile?

La solution

Your problem in the response encoding to JSON as it can't parse datetime(date) to JSON, you should convert it to string using strftime function.

so you can try:

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")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top