5๐
โ
@api_view
decorator missing.To get request.data
in DRF, either you have to use the decorator @api_view
or extend from the APIView
class
if you are using class-based view.
from rest_framework.decorators import api_view
@api_view
def list(request):
if request.method == 'POST':
serializer = CustomerSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
For class based view extend from APIView
from rest_framework.views import APIView
class List(APIView):
def post(self, request):
serializer = CustomerSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
documentation : DRF request
๐คAneesh R S
Source:stackexchange.com