Answers for "convert queryset to json django"

1

convert queryset to json django

from django.core import serializers
from django.http import HttpResponse

def some_view(request):
    qs = SomeModel.objects.all()
    qs_json = serializers.serialize('json', qs)
    return HttpResponse(qs_json, content_type='application/json')
Posted by: Guest on August-23-2021
1

convert queryset to json django

from django.http import JsonResponse

def some_view(request):
    data = list(SomeModel.objects.values())  # wrap in list(), because QuerySet is not JSON serializable
    return JsonResponse(data, safe=False)  # or JsonResponse({'data': data})
Posted by: Guest on August-23-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language