0

In Django view, I am trying this-

@csrf_exempt
@api_view(['GET', 'POST'])
def EmployeeList(request):

    if request.method == 'GET':
        employees = PersonalInfo.objects.all()
        serializer = PersonalInfoSerializer(employees, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        print data
        serializer = PersonalInfoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

On posting data from postman, It raising attribute error

    {
    "detail": "JSON parse error - No JSON object could be decoded"
}

when using data = JSONParser().parse(request.data) instead of data = JSONParser().parse(request) getting this error :

AttributeError: 'QueryDict' object has no attribute 'read'

1 Answer 1

1

when you use drf the request is instance of rest_framework.request.Request requests, but for rest_framework.parsers JSONParser.parser you need to send as parameter: django.core.handlers.wsgi.WSGIRequest, which you can get by request._request, but i think only you need is:

 serializer = PersonalInfoSerializer(data=request.data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.