diff --git a/accounts/serializers.py b/accounts/serializers.py index bde6340..6accfde 100644 --- a/accounts/serializers.py +++ b/accounts/serializers.py @@ -70,11 +70,12 @@ def validate(self, data): class ProfileSerializer(serializers.ModelSerializer): - friend_count = serializers.SerializerMethodField() + friend_count = serializers.SerializerMethodField(read_only=True) class Meta: model = User fields = ["name", "reliability", "profile_img", "friend_count"] + read_only_fields = ["reliability", "friend_count"] def get_friend_count(self, obj): return Friend.objects.filter(user=obj).count() diff --git a/accounts/views.py b/accounts/views.py index 9d3e130..d138aa3 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -35,7 +35,7 @@ def post(self, request): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) -@api_view(["GET"]) +@api_view(["GET", "PATCH"]) # @authentication_classes([TokenAuthentication]) # @permission_classes([IsAuthenticated]) def profile(request): @@ -46,6 +46,13 @@ def profile(request): serializer = ProfileSerializer(user) return Response(serializer.data) + elif request.method == "PATCH": + serializer = ProfileSerializer(user, data=request.data, partial=True) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + return Response({"error": "Invalid data"}, status=status.HTTP_400_BAD_REQUEST) + return Response( {"error": "Unsupported method"}, status=status.HTTP_405_METHOD_NOT_ALLOWED )