Search code examples
pythonpython-2.7odooopenerp-7

Openerp 7 - raise TypeError(repr(o) + " is not JSON serializable")


I am trying to set default value to one of my datetime field using a function and that function returning a datetime value as well.

The issue is whenever I tried to create a new record (error issuing point) it gives me this error message and I have no idea about solving this. Please help me with this.

Error enter image description here

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

The Fields

      'date_from':fields.datetime(string='Date From'),
      'date_to':fields.datetime(string='Date To'),

 _defaults = {_getLastFromTime}

The function

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

Solution

  • Your function has to return a string in a format Odoo needs.

    There are some nice methods in fields.Date and fields.Datetime to convert python date(time) to Odoo compatible strings or the other way (Odoo 8+).

    And there are some other methods, too.

    Date

    Date.today() - returns today in utc as string compatible to Odoo date fields

    Date.context_today() - returns today in user timezone as string compatible to Odoo date

    Date.from_string() - converts an Odoo date string to a python date object

    Date.to_string() - reversed from_string

    Datetime

    Simalar to the Date methods: Datetime.now(), Datetime.context_timestamp(), Datetime.from_string(), Datetime.to_string()