Your problem is not related to dates. It is just that the requests are not directly serializable, even with the DjangoJSONEncoder class and even using values()
. You will get exactly the same result with a model without date and time fields.
The way you should do serialization in Django is to use serializers
:
from django.core import serializers output = serializers.serialize('json', MyModel.objects.all())
But, no doubt, you are trying to avoid this, because the output format is so unnecessarily complex. Instead, I usually use the python serializer to convert to a dict, and then dump json:
temp_output = serializers.serialize('python', MyModel.objects.all()) output = json.dumps(temp_output, cls=DjangoJSONEncoder)
source share