我正在尝试使用函数将默认值设置为 datetime 字段之一,并且该函数也会返回日期时间值。
问题在于,每当我尝试创建新记录(错误发布点)时,它都会给我这个错误消息,而我不知道解决这个问题。请帮帮我。
2018-02-15 05:47:30,317 11190 INFO new23012018 werkzeug: 127.0.0.1 - - [15/Feb/2018 05:47:30] "POST /web/dataset/call_kw/hr.analytic.timesheet:default_get HTTP/1.1" 500 -
2018-02-15 05:47:30,321 11190 ERROR new23012018 werkzeug: Error on request:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 193, in run_wsgi
execute(self.server.app)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 181, in execute
application_iter = app(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/service/wsgi_server.py", line 417, in application
return application_unproxied(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/service/wsgi_server.py", line 403, in application_unproxied
result = handler(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 532, in __call__
return self.dispatch(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 491, in __call__
return self.app(environ, start_wrapped)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 599, in __call__
return self.app(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 491, in __call__
return self.app(environ, start_wrapped)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 599, in __call__
return self.app(environ, start_response)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 557, in dispatch
result = handler(request)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 622, in <lambda>
return lambda request: JsonRequest(request).dispatch(method)
File "/home/manisha/HR_Workspace/openerp-7.0/openerp/addons/web/http.py", line 251, in dispatch
body = simplejson.dumps(response)
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 354, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 262, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 340, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 239, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2018, 2, 15, 4, 21, 29) is not JSON serializable
字段
'date_from':fields.datetime(string='Date From'),
'date_to':fields.datetime(string='Date To'),
_defaults = {_getLastFromTime}
功能
def _getLastFromTime(self, cr, uid, context=None):
tsht_ids=self.search(cr, uid, [('user_id', '=', uid)], context=context)
tsht_strt_tme=datetime.datetime.now()
if tsht_ids:
last_id=max(tsht_ids)
tsht_time=self.browse(cr, uid, tsht_ids[0], context=context).date_to
tsht_strt_tme=datetime.datetime.strptime(tsht_time, '%Y-%m-%d %H:%M:%S')
else:
return False
return tsht_strt_tme
答案 0 :(得分:1)
你的函数必须以Odoo需要的格式返回一个字符串。
fields.Date
和fields.Datetime
中有一些很好的方法可以将python日期(时间)转换为Odoo兼容字符串或其他方式(Odoo 8 +)。
还有一些其他方法。
Date.today()
- 今天在utc中返回与Odoo日期字段兼容的字符串
Date.context_today()
- 今天在用户时区返回与Odoo日期
Date.from_string()
- 将Odoo日期字符串转换为python日期对象
Date.to_string()
- 反转from_string
Simalar到日期方法:Datetime.now()
,Datetime.context_timestamp()
,Datetime.from_string()
,Datetime.to_string()