Rakesh Rakesh - 2 years ago 257
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 Source

This solved my problem.

import json
from django.core.serializers.json import DjangoJSONEncoder
data =  json.dumps(data, cls=DjangoJSONEncoder)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download