def get(self, request, user_id=None): if user_id is not None: user = User.objects.get(id=user_id) serializer = UserSerializer(user, many=False) return Response(serializer.data) else: users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data)
def post(self, request): serializer = UserSerializer(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 put(self, request, user_id): #getting the category, then pass the data user = User.objects.get(id=user_id) serializer = UserSerializer(user, 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)
class UserRetrieveUpdateAPIView(APIView): # Allow only authenticated users to access this url permission_classes = (IsAuthenticated,) serializer_class = UserSerializer @swagger_auto_schema( responses = {status.HTTP_200_OK : UserSerializer(many=True)} ) def get(self, request, *args, **kwargs): # serializer to handle turning our `User` object into something that # can be JSONified and sent to the client. serializer = self.serializer_class(request.user) return Response(serializer.data, status=status.HTTP_200_OK)