1

我正在尝试计算截止日期。这是代码:

 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)

字段到期日期是日期类型

我收到此错误:

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>

我在没有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

请有人能告诉我我在哪里做错了吗?谢谢

4

1 回答 1

0

您对 JSON 的响应编码存在问题,因为它无法将 datetime(date) 解析为 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")
于 2014-03-01T23:37:07.760 回答