showing results for - "convert queryset to json django"
Maja
17 Mar 2020
1from django.core import serializers
2from django.http import HttpResponse
3
4def some_view(request):
5    qs = SomeModel.objects.all()
6    qs_json = serializers.serialize('json', qs)
7    return HttpResponse(qs_json, content_type='application/json')
8
Mia
02 Jul 2016
1from django.http import JsonResponse
2
3def some_view(request):
4    data = list(SomeModel.objects.values())  # wrap in list(), because QuerySet is not JSON serializable
5    return JsonResponse(data, safe=False)  # or JsonResponse({'data': data})
6