def post(self, request): serializer = ProviderSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def provider_list(request): if request.method == 'GET': provider = Provider.objects.all() serializer = ProviderSerializer(provider, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ProviderSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def provider_detail(request, pk): try: provider = Provider.objects.get(pk=pk) except Provider.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': provider_detail = Provider.objects.get(pk=pk) serializer = ProviderSerializer(provider_detail) return Response(serializer.data) elif request.method == 'PUT': serializer = ProviderSerializer(provider, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': provider.delete() return Response(status=status.HTTP_204_NO_CONTENT)