How do I get DateTimeField
from MySQL 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'
.
DjangoJSONEncoder solved my problem.
import json
from django.core.serializers.json import DjangoJSONEncoder
data = json.dumps(data, cls=DjangoJSONEncoder)
Django's serializers are only meant to be used on query sets; you will have to find a different way to solve your problem, such as converting the datetime
to something else first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With