def post(self, request, *args, **kwargs): request_data = request.data ser = UserSerializer(data=request_data) ser.is_valid(raise_exception=True) user_obj = ser.save() return APIResponse(200, True, results=UserSerializer(user_obj).data)
def User_list(request): if request.method == "GET": users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data) elif request.method == "POST": print(request.body) serializer = UserSerializer(data=json.load(request.body)) 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 User_detail(request, pk): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.methond == "GET": serializer = UserSerializer(user) return Response(serializer.data) elif request.method == "PUT": serializer = UserSerializer(user, data=json.loads(request.body)) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND) elif request.method == "DELETE": user.delete() return Response(status=status.HTTP_204_NO_CONTENT)