Rakesh Rakesh - 6 months ago 98
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'

Answer

This solved my problem.

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