Skip to content

Commit b7a8740

Browse files
refactor(account): Use UserProfileSerializer for /me endpoint
- Modified the "me" endpoint in UserViewSet to correctly use the UserProfileSerializer with the user's profile object - Added request context to the serializer for proper URL resolution of image fields - Updated the update logic to use serializer.save() to invoke the custom update method in UserProfileSerializer - Improves handling of both User and Profile data in a single endpoint
1 parent c0ce540 commit b7a8740

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

account/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class UserProfileSerializer(serializers.ModelSerializer):
2121
profile_pic = serializers.ImageField(
2222
required=False,
2323
help_text="Upload a profile picture (max size 2MB).",
24+
use_url=True,
2425
)
2526

2627
class Meta:

account/views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,23 @@ def me(self, request, *args, **kwargs):
7070
Manage operations on the authenticated user's profile.
7171
"""
7272
try:
73+
# Get the user's profile for serialization
74+
profile = request.user.profile
75+
7376
if request.method == "GET":
74-
serializer = self.get_serializer(request.user)
77+
serializer = UserProfileSerializer(profile, context={'request': request})
7578
return Response({
7679
"message": "Profile retrieved",
7780
"data": serializer.data
7881
}, status=status.HTTP_200_OK)
7982

8083
elif request.method in ["PUT", "PATCH"]:
8184
partial = request.method == "PATCH"
82-
serializer = self.get_serializer(
83-
request.user, data=request.data, partial=partial
85+
serializer = UserProfileSerializer(
86+
profile, data=request.data, partial=partial, context={'request': request}
8487
)
8588
serializer.is_valid(raise_exception=True)
86-
self.perform_update(serializer)
89+
serializer.save() # This calls the update method on the serializer
8790
return Response({
8891
"message": "Profile updated",
8992
"data": serializer.data

0 commit comments

Comments
 (0)