JsonResponse is from vanilla django.
Response is from restframework.
I have code in the following respectively.
from django.http.response import JsonResponse
from book.models import Publisher
from book.serializers import PublisherSerializer
def get_publisher(publisher):
publisher_serializer = PublisherSerializer(publisher)
return JsonResponse(publisher_serializer.data)
from rest_framework.response import Response
from book.models import Publisher
from book.serializers import PublisherSerializer
def get_publisher(publisher):
publisher_serializer = PublisherSerializer(publisher)
return Response(publisher_serializer.data)
What are the differences?
In the settings, I have REST_FRAMEWORK overwritten.
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'utils.api.renderers.CamelCaseJSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'utils.api.parsers.CamelCaseJSONRenderer',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
}
That is the conversion between snake case and camel case for all the keys of json.
In the python, we use snake case for all the variable naming.
In the json, the convention is camel case.
In order to have the case conversion work, the Response from rest_framework.response should be used in here.