Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: Object of type QuerySet is not JSON serializable

I am trying to remove items from cart using Jquery and Django, i have written the logic to do this but i keep getting this errror that says Object of type QuerySet is not JSON serializable, i can seem to know what the problem is because i have tried converting the code below to list using list() method in django and also values() but it still does not work as expected.

views.py

def RemoveWishlist(request):
    wishlist = Wishlist.objects.filter(user=request.user)
    ...
    context = {
        "bool":True,
        "wishlist":wishlist
    }
    t = render_to_string('core/async/wishlist-list.html', context)
    return JsonResponse({'data':t,'wishlist':wishlist})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As the error mentions, queryset is not JSON serializable. Hence you can make it into JSON using Django’s serializers and pass it to JsonResponse class initiation:

from django.core import serializers
....
qs_json = serializers.serialize('json', wishlist)
return JsonResponse({'data':t,'wishlist':qs_json})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading