pagination comment

right type of comment response
This commit is contained in:
Parsa Nazer
2025-01-27 17:16:47 +03:30
parent e48b2587cf
commit 7a0478dcde
2 changed files with 26 additions and 4 deletions
+1 -2
View File
@@ -5,6 +5,5 @@ urlpatterns = [
path('', AllProductsView.as_view(), name='category-products'),
path('categories', AllCategories.as_view(), name='all-categories'),
path('<int:pk>', ProductView.as_view(), name='product-detail'),
path('<int:pk>/comments', CommentView.as_view(), name='product-comments'),
path('comments/<int:pk>', CommentView.as_view(), name='comment-delete'),
path('comments/<int:pk>', CommentView.as_view(), name='comment-views'),
]
+25 -2
View File
@@ -196,11 +196,34 @@ class AllProductsView(APIView):
class CommentView(APIView):
serializer_class = CommentSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
pagination_class = StructurePagination
@extend_schema(
parameters=[
OpenApiParameter(
name="limit",
description="Number of results to return per page (pagination).",
required=False,
type=OpenApiTypes.INT,
),
OpenApiParameter(
name="offset",
description="The starting position of the results (pagination).",
required=False,
type=OpenApiTypes.INT,
)
],
responses={
200: CommentSerializer(many=True),
404: OpenApiTypes.OBJECT,
},
)
def get(self, request, pk):
product = get_object_or_404(ProductModel, id=pk)
comments = product.comments.filter(show=True)
comments_ser = self.serializer_class(instance=comments, many=True)
return Response({'comments': comments_ser.data}, status=status.HTTP_200_OK)
paginator = self.pagination_class()
paginated_comments = paginator.paginate_queryset(comments, request)
comments_ser = self.serializer_class(instance=paginated_comments, many=True)
return paginator.get_paginated_response(comments_ser.data)
def post(self, request, pk):
comment_ser = CommentSerializer(data=request.data)