k neha
k neha

Reputation: 25

how to send send the data using python django?

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

Upvotes: 0

Views: 86

Answers (2)

Shahid Tariq
Shahid Tariq

Reputation: 931

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})

Upvotes: 1

kamilyrb
kamilyrb

Reputation: 2627

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'})

Upvotes: 1

Related Questions