Categories
Python Answers

How to fix Django object is not JSON serializable with Python Django?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *