Unable to display queryset using ajax and Django Unable to display queryset using ajax and Django ajax ajax

Unable to display queryset using ajax and Django


If you look at the error message from Django, you will see it complaining that the queryset is not JSON serializable. For ajax requests, you can see the response using your web browser's development tools when DEBUG=True.

The first thing to do is to use values(), which returns a queryset containing dictionaries for each instance in the queryset. Secondly, you need to coerce the queryset into a list.

queryset = StoreProduct.objects.get_size(productid)values_list = list(queryset.values())


You cannot send queryset directory as json, because json is just a string. You could use django serializer to apply to your queryset:

from django.core import serializersserialized_qs = serializers.serialize('json', queryset)data = {"queryset" : serialized_qs}return JsonResponse(data)

Also in your javascript you have to do data['queryset'] to access your queryset as json.