diff --git a/apps/post/api_views.py b/apps/post/api_views.py index 4df3d60..cd06ccf 100644 --- a/apps/post/api_views.py +++ b/apps/post/api_views.py @@ -5,6 +5,7 @@ from rest_framework import status from rest_framework.permissions import IsAuthenticated from rest_framework.pagination import PageNumberPagination +from rest_framework.decorators import action from apps.post import models, serializers, mixins, permissions @@ -52,6 +53,19 @@ def list(self, request, *args, **kwargs): .order_by('-rating', '-upvotes')) return super().list(self, request, *args, **kwargs) + @action(detail=True, methods=['GET'], url_path='comments') + def comments(self, request, *args, **kwargs): + obj = self.get_object() + queryset = obj.comment_set.all() + + page = self.paginate_queryset(queryset) + if page is not None: + serializer = serializers.CommentModelSerializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + class CategoryViewSet(mixins.DenyDeletionOfDefaultCategoryMixin, viewsets.ModelViewSet): diff --git a/apps/post/serializers.py b/apps/post/serializers.py index 90d63c1..46c2590 100644 --- a/apps/post/serializers.py +++ b/apps/post/serializers.py @@ -55,7 +55,6 @@ class PostModelSerializer(serializers.ModelSerializer): user_id = UserModelSerializer() userUpVotes = UserModelShortSerializer(many=True) userDownVotes = UserModelShortSerializer(many=True) - comments = CommentModelSerializer(source="comment_set", many=True) class Meta: model = models.Post @@ -67,7 +66,6 @@ class Meta: "user_id", "cat_id", "tags", - "comments", "userUpVotes", "userDownVotes", "created_at",