pagination comment
right type of comment response
This commit is contained in:
@@ -5,6 +5,5 @@ urlpatterns = [
|
|||||||
path('', AllProductsView.as_view(), name='category-products'),
|
path('', AllProductsView.as_view(), name='category-products'),
|
||||||
path('categories', AllCategories.as_view(), name='all-categories'),
|
path('categories', AllCategories.as_view(), name='all-categories'),
|
||||||
path('<int:pk>', ProductView.as_view(), name='product-detail'),
|
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-views'),
|
||||||
path('comments/<int:pk>', CommentView.as_view(), name='comment-delete'),
|
|
||||||
]
|
]
|
||||||
@@ -196,11 +196,34 @@ class AllProductsView(APIView):
|
|||||||
class CommentView(APIView):
|
class CommentView(APIView):
|
||||||
serializer_class = CommentSerializer
|
serializer_class = CommentSerializer
|
||||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
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):
|
def get(self, request, pk):
|
||||||
product = get_object_or_404(ProductModel, id=pk)
|
product = get_object_or_404(ProductModel, id=pk)
|
||||||
comments = product.comments.filter(show=True)
|
comments = product.comments.filter(show=True)
|
||||||
comments_ser = self.serializer_class(instance=comments, many=True)
|
paginator = self.pagination_class()
|
||||||
return Response({'comments': comments_ser.data}, status=status.HTTP_200_OK)
|
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):
|
def post(self, request, pk):
|
||||||
comment_ser = CommentSerializer(data=request.data)
|
comment_ser = CommentSerializer(data=request.data)
|
||||||
|
|||||||
Reference in New Issue
Block a user