Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of type QuerySet is not JSON serializable Django

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)
like image 743
vipul Avatar asked Oct 23 '25 02:10

vipul


2 Answers

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})
like image 200
Hugo Martin Avatar answered Oct 25 '25 15:10

Hugo Martin


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)
like image 45
MattG Avatar answered Oct 25 '25 17:10

MattG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!