0

How to send the json data using post request in django python. I have a code for get the data and I don't use any html file.

class employeeList(APIView):
    def get(self, request):
        employee1 = employees.objects.all()
        serializer = employeeSerializer(employee1, many=True)
        return Response(serializer.data)
    def post(self,request):
        pass

can you please help for post request.Now I want to create post function to send the data

1

2 Answers 2

1

You can use this basically,

def post(self, request):
    serializer = employeeSerializer(data=request.data)  
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response({'message':'Data davet succesfully'})
Sign up to request clarification or add additional context in comments.

5 Comments

I have another dought can you explain it
Sure, what is the problem?
I want update the data using Id value.I will try but I didn't get it
can you please give the solution
You can use put() method like post method.But your serializer must contains pk field.Or you can use UpdateAPIView. it is very usefull. You can find other drf generic views here
1

You can simply dump query set into Json format like:

def post(self, request):
    data = list(employees.objects.values())
    return JsonResponse(data, safe=False)  
               # or 
    return JsonResponse({'data': data})

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.