Saturday, September 1, 2012

django JSON with DateTime



Django json.dump(data) throws error ‘datetime (...) is not JSON serialzable.’ because default it only does queryset json serialization. Use below to serialize dates.


from django.core.serializers.json import DjangoJSONEncoder


def test(request, title):
    …
    data =  json.dumps(qset, cls=
DjangoJSONEncoder)

Which is similar to extending the default JSONEncoder and check for the datetime and return it with extra code to handle it.

This should resolve the issue.

No comments:

Post a Comment