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)
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})
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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.