Odoo Help


How to return date from a function

on 4/23/14, 7:57 AM 2,554 views

Thanks in advance ..

def onchange_loan_id(self, cr, uid, ids, loan_id, context=None):
        if loan_id:
            name = self.pool.get('bank.new_loan').browse(cr, uid, loan_id, context)
            due_date_of_installment=datetime.datetime.strptime(name.loan_due_date, '%Y-%m-%d')
            next_due_date= (due_date_of_installment+datetime.timedelta(1*365/12)+datetime.timedelta(days=1))
            return {'value':{'name':name.name.name,'due_date_of_installment':due_date_of_installment,'number_of_installment':total_installment,'installment_amount':monthly_installment,'next_installment_due_date':next_due_date}}

Out put is raise TypeError(repr(o) + " is not JSON serializable") TypeError: datetime.datetime(2014, 5, 22, 0, 0) is not JSON serializable Cannot return dates...please help

Hi, do u need the date with time in particular??

Dhinesh - Technical Consultant, Sodexis Inc
on 4/23/14, 8:17 AM
Roc cheng
On 4/23/14, 10:25 AM

Hi, In my code:

_columns = {
        'date_end': fields.date('Date End', required=True),
_defaults ={
        'date_end': str(datetime.datetime.now().date()),

datetime.datetime.now().date() get the date tye,then str()


Hi, Python 2.7 cannot handle dates

from datetime import datetime
import json

data = {
    'title': 'Hello World',
    'created_on': datetime(2013, 4, 23, 0, 0)
print json.dumps(data)

This would create error like TypeError: datetime.datetime(2013, 4, 23, 0, 0) is not JSON serializable

Now try this in custom function like

from datetime import datetime
import json

data = {
    'title': 'Hello World',
    'created_on': datetime(2013, 4, 23, 0, 0)

def date_handler(obj):
    return obj.isoformat() if hasattr(obj, 'isoformat') else obj

print json.dumps(data, default=date_handler)

Now the result will be {"created_on": "2013-04-23T00:00:00", "title": "Hello World"}

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 4/23/14, 7:57 AM
Seen: 2554 times
Last updated: 3/16/15, 8:10 AM