Sometimes, we want to fix Django object is not JSON serializable with Python Django.
In this article, we’ll look at how to fix Django object is not JSON serializable with Python Django.
How to fix Django object is not JSON serializable with Python Django?
To fix Django object is not JSON serializable with Python Django, we can serialize the object with Django’s built in serializers.
For instance, we write
data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")
to call serializers.serialize with 'json' and a query set to serialize the queryset into JSON.
And then we create a HttpResponse object with the serialized data and return that as the response in our view function.
Conclusion
To fix Django object is not JSON serializable with Python Django, we can serialize the object with Django’s built in serializers.