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)

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.

Whenever you see an error message of the form ... object has no attribute '_meta' that is a clear-cut sign that a method was expecting a queryset but got something else. In this particular situation, serializers.serialize must have a queryset for its second argument. You can't use a list, dictionary, etc., and definitely not a string.

Where is data being set. Check to make sure it's being assigned a true queryset. You might also want to post more of your code, if you're still having issues. It's difficult to diagnose the problem much more, out of context.

Need Your Help

Java Reflection: Getting fields and methods in declaration order

java reflection junit

Is there any way to get a classes declared fields (and methods) in the order of declaration using reflection? According to the documentation, the ordering of Methods and Fields returned by getField...

IP Fragmentation and Reassembly

networking tcp network-protocols fragmentation defragmentation

I am currently going through my networking slides and was wondering if someone could help me with the concept of fragmentation and reassembly.