Server/Django

[django] User Profile Response 요청하기


in serializers.py

from .models import Profile

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__'

 

in views

from .serializers import ProfileSerializer

class ProfileAPI(viewsets.ViewSet):

    def retrieve(self, request, pk=None):
        user = get_object_or_404(User, pk=pk)
        profile_serializer = ProfileSerializer(user.profile)
        return Response(profile_serializer.data)

 

in urls.py

from .views import ProfileAPI

profile_detail = ProfileAPI.as_view({
    'get': 'retrieve',
})



urlpatterns = [
    ...
    path('<int:pk>/profile/', profile_detail, name='profile-detail'),
]