Rakesh Rakesh - 7 months ago 123
Python Question

serialize datetime to json in Django

Hi how do i get DateTimeField from mysql database in django in json format. I got an error when i executed the code 'Date.time cannot be serialized in json'(data holds a lot of values)

data = json.dumps(data)

but this was fixed by adding

ALL_data = serializers.serialize("json", data, ensure_ascii=False)

But now i get 'str' object has no attribute '_meta'


This solved my problem.

import json
from django.core.serializers.json import DjangoJSONEncoder
data =  json.dumps(data, cls=DjangoJSONEncoder)