Odoo Help


This community 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.


How to return date from a function

on 4/23/14, 7:57 AM 1,829 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()


Dhinesh - Technical Consultant, Sodexis Inc

--Dhinesh - Technical Consultant, Sodexis Inc--
| 5 4 9
Pondicherry, India
--Dhinesh - Technical Consultant, Sodexis Inc--

Passionate coder in Python/OpenERP. Knows Django/Flask MVC frameworks. Did code in Java, PHP.

Contact me at: dvdhinesh.mail@gmail.com

Dhinesh - Technical Consultant, Sodexis Inc
On 4/23/14, 8:33 AM

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"}

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


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