Reputation: 87
When I am trying to send values in JsonResponse then The error is coming(object of type QuerySet is not JSON serializable )
def ajaxAgent(request):
data = CommCenter.objects.values()
responseData = { 'status': 'success', 'msg' : data}
return JsonResponse(responseData)
Upvotes: 4
Views: 13002
Reputation: 1932
What worked for me was using values_list()
and converting to list using list
def ajaxAgent(request):
data = CommCenter.objects.filter().values_list()
responseData = { 'status': 'success', 'msg' : list(data)}
return JsonResponse(responseData)
Upvotes: 2
Reputation: 167
Please find here the answer:
from django.http import JsonResponse
def some_view(request):
data = list(SomeModel.objects.values())
return JsonResponse(data, safe=False) # or JsonResponse({'data': data})
Upvotes: 7
Reputation: 269
You will have to write a model serializer to serialize the values of the objects into JSON that are returned to you as list when you fetch objects using Django ORM. Check this link out for more details ModelSerializer
Upvotes: 0