From 3cd7228c05ab775533989e5f85225eab940288fd Mon Sep 17 00:00:00 2001 From: jhynsoo Date: Wed, 1 Nov 2023 01:14:02 +0900 Subject: [PATCH] feat: Add my posts view --- market/viewsets.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/market/viewsets.py b/market/viewsets.py index dd016da..46451d5 100644 --- a/market/viewsets.py +++ b/market/viewsets.py @@ -71,6 +71,19 @@ class PostViewset(ActionBasedMixin, ModelViewSet): "create": [AllowAny], } + @action(detail=False, methods=["GET"]) + def my(self, request): + user = request.user + queryset = user.posts.all() + + page = self.paginate_queryset(queryset) + if page is not None: + serializer = PostListSerializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = PostListSerializer(queryset, many=True) + return Response(serializer.data) + def perform_create(self, serializer): serializer.save(author=self.request.user)